gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:56:51 24 4
gpt4 key购买 nike

我的类型:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com