我的类型:
open System
type MyType (myFn : unit -> bool) =
member this.MyResult with get () = myFn ()
new (myFn : Func<bool>) = MyType (myFn.Invoke)
我得到的错误:
A unique overload for method 'MyType' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: new : myFn:(unit -> bool) -> MyType, new : myFn:Func<bool> -> MyType
既然我已经提供了类型注释,那么这不起作用的真正原因是什么?
最佳答案
如果 F# 采用 Func<bool>
将能够调用构造函数代表。因此,使用需要 Func<bool>
的单个重载应该适用于 C# 和 F#:
open System
type MyType (myFn : Func<bool>) =
member this.MyResult with get () = myFn.Invoke ()
关于c# - F#:尝试支持在 F# 和 C# 习惯用法的构造函数中传递函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114748/