且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么F#的类型推断不能解决这个问题?

更新时间:2023-12-04 15:02:46

类型推断从左到右起作用.这是管道运算符有用的地方;如果您已经知道"FIS"的类型,则将其写为

Type inference works left-to-right. This is where the pipeline operator is useful; if you already know the type of 'fis', then write it as

fis |> Seq.map (fun fi -> fi.Name)

然后推论对您有用.

(通常是以下形式的表达式

(In general, expressions of the form

o.Property
o.Method args

要求先验已知的'o'的类型;对于大多数其他表达式,当没有固定类型时,推理系统可以浮动一个约束",以后可以解决,但对于这些情况,不存在所有具有名为P的属性的类型"或"所有带有名为M的方法的类型"(例如鸭类输入法)都可以推迟,以后再解决.因此,您现在需要该信息,否则推断将立即失败.)

require the type of 'o' to be known a priori; for most other expressions, when a type is not pinned down the inference system can 'float a constraint' along that can be solved later, but for these cases, there are no constraints of the form 'all types with a property named P' or 'all types with a method named M' (like duck typing) that can be postponed and solved later. So you need that info now, or inference fails immediately.)

另请参见 F#中类型推断的概述.