F#类型推断仅适用于与F#相关的类型,但类或接口除外。 但是我不知道为什么。我知道候选人会增加,但这不可能吗?还有其他原因吗?
答案 0 :(得分:1)
在许多情况下,根本无法确定对象的类型。最基本的情况是:
type A () = member x.bar () = ()
type B () = member x.bar () = ()
let foo x = x.bar () // Is x A or B?
尽管编译器会尽力而为,所以如果知道使用时的类型,它将很高兴地允许您跳过注释:
type A () = member x.bar () = ()
type B () = member x.bar () = ()
let blah (x: A) = x.bar ()
let foo x =
blah x
x.bar () // x is known to be A thanks to above line
在我的旧问题中,可以找到更深入的讨论:Why is type inference impractical for object oriented languages?