c# - F#:尝试支持在 F# 和 C# 习惯用法的构造函数中传递函数

标签 c# constructor f# overloading

我的类型:

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/

相关文章:

c# - 自动映射器用前缀展开

constructor - 如何使用工厂构造函数在 Dart 中实现单例模式?

javascript - 创建远离其构造函数的新对象实例

.net - FSharpChart 空白输出

c# - CSS 在页面回发 ASP.NET 期间被删除了一段时间

c# - 在 TLS : The remote server return an error : 234 AUTH TLS OK 上使用 FtpWebRequest 的问题

c++ - 不做任何事情的结构的构造函数

c# - F# 度量单位的反射(reflect)

c# - TreeView 节点中的多行?

c# - 具有Media Player组件的C#中的MP3音频流