Unknown need for type annotation or cast

后端 未结 2 1221
长情又很酷
长情又很酷 2021-01-23 04:22

I know I must be missing something really obvious here. B.GetInstance().Call() generates the error: Lookup on object of indeterminate type based on information

2条回答
  •  粉色の甜心
    2021-01-23 04:30

    Frequently when you've got a recursive set of methods whose types to infer, F# needs help. A more pleasant alternative would be to annotate the definition of B.GetInstance:

    type A() =
      member x.Call() = B.GetInstance().Call()
    
    and B() =
      static member GetInstance() : B = new B()
      member x.Call() = ()
    

    I believe that the reason you run into this problem is that F# tries to solve all inferred types on all methods in A and B simultaneously (because they are defined as mutually recursive types), and this leads to problems, but perhaps someone from the F# team will weigh in.

提交回复
热议问题