[英]F#: how to specify static overload to use?
我有两个用C#代码声明的类:
public class A
{
public static bool TryParse(string value, out A result)
{
...
}
}
public class B : A
{
public static bool TryParse(string value, out B result)
{
...
}
}
从C#调用B.TryParse
是没有问题的,因为正确的重载由out参数类型确定,应该事先声明。 由于out参数在F#中被转换为部分结果,我们得到两个具有相同参数签名的函数...并且从F#调用导致A unique overload for method 'TryParse' could not be determined based on type information prior to this program point. A type annotation may be needed.
A unique overload for method 'TryParse' could not be determined based on type information prior to this program point. A type annotation may be needed.
错误。 我理解这个问题,甚至会将TryParse
声明为new
......如果它不是静态的。
消息本身并不是很有用:它绝对不能清楚哪种注释和添加位置。
我该怎么办电话? 最愚蠢的想法是重命名其中一个功能,但可能还有更聪明的方法吗?
您需要向传递给try方法的变量添加类型注释,在本例中为out变量,因为这是更改的内容。 我认为这样的事情应该有效:
let res, (b:B) = B.TryParse "MyString"
在F#类型中,注释位于标识符之后,并以冒号(:)开头。
(注意:在F#out参数中可以从结果中恢复为元组)
有几种方法可以解决这个问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.