F#:如何指定要使用的静态重载? [英] F#: how to specify static overload to use?
问题描述
我有两个用C#代码声明的类:
I have two classes declared in C# code:
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参数类型确定,该参数类型应事先声明.由于在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.
错误.我了解这个问题,甚至可以将TryParse
声明为new
...如果它不是静态的.
While calling B.TryParse
from C# isn't problem, because correct overload is determined by out parameter type, which should be declared in advance. As out parameter is transformet to part of result in F#, we got two function with same parameter signature... And call from F# causes 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.
error. I understand the issue, and would even declare TryParse
as new
... If it weren't static.
消息本身不是很有帮助:绝对不能清除注释类型和添加位置.
The message itself is not very helpful: it's absolutely not cleare what kind of annotation and where to add.
我怎么打这个电话? 最愚蠢的想法是重命名功能之一,但是可能还有更聪明的方法吗?
How can I do this call? Most stupid idea is to rename one of functions, but may be there are more clever way?
推荐答案
您需要在要传递给try方法的变量中添加类型注释,在这种情况下,out变量将改变.我认为类似这样的方法应该起作用:
You need to add a type annotation to the variables you are passing to the try method, in this case the out variable as this is the thing that changes. I think something like this should work:
let res, (b:B) = B.TryParse "MyString"
在F#类型中,注释在标识符之后,并以冒号(:)开头.
In F# type annotations come after the an identifier and are preceded by a colon (:).
(注意:in F#out参数可以从结果中作为元组恢复)
(Note: in F# out parameters can be recovered as tuple from the result)
这篇关于F#:如何指定要使用的静态重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!