Mil*_*oDC 3 c# f# constructor overloading
我的风格:
open System
type MyType (myFn : unit -> bool) =
member this.MyResult with get () = myFn ()
new (myFn : Func<bool>) = MyType (myFn.Invoke)
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
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
既然我已经提供了类型注释,那么这个不起作用的真正原因是什么?
如果它接受Func<bool>委托,F#将能够调用构造函数.因此,使用单个重载Func<bool>应该适用于C#和F#:
open System
type MyType (myFn : Func<bool>) =
member this.MyResult with get () = myFn.Invoke ()
Run Code Online (Sandbox Code Playgroud)