未知需要类型注释或强制转换 [英] Unknown need for type annotation or cast

查看:108
本文介绍了未知需要类型注释或强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我一定在这里确实缺少一些明显的东西. B.GetInstance().Call()生成错误:基于此程序点之前的信息查找不确定类型的对象.在此程序指向之前可能需要类型注释,以约束对象的类型.这样可以解决查找问题.

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 prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.

我正在使用v1.9.9.9.

I'm using v1.9.9.9.

type A() =
    member x.Call() = B.GetInstance().Call()

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

我刚刚发现这可行:(B.GetInstance() :> B).Call()

知道为什么需要强制转换吗?

Any idea why the cast is necessary?

推荐答案

经常有一组递归的方法可以推断其类型,F#需要帮助.一个更令人愉快的选择是注释B.GetInstance:

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() = ()

我相信您遇到此问题的原因是F#尝试同时解决A和B中所有方法上的所有推断类型(因为它们被定义为相互递归类型),这会导致问题,但也许有人来自F#团队的人员将参与进来.

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.

这篇关于未知需要类型注释或强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆