开发者

Problem with overloaded methods in F#

Why code like this doesn't compile?

type MyType() =
    static member myMember (f,(g,h)) = g
    static member myMember (a,(b,c,d)) = MyType.myMember(a,(b,c))
    static member myMember (a,(b,c,d,e)) = MyType.myMember(a,(b,c))

It says: ... The tuples have differing 开发者_高级运维lengths of 2 and 4

It is a bug?


I'm not sure if it's a bug, but generally speaking type inference in the presence of multiple overloads is hard to do well. Your best bet is to add some type annotations:

type MyType() =
  static member myMember<'a,'b,'c> (f:'a,(g:'b,h:'c)) = g
  static member myMember (a,(b,c,d)) = MyType.myMember<_,_,_>(a,(b,c))
  static member myMember (a,(b,c,d,e)) = MyType.myMember<_,_,_>(a,(b,c))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜