gpt4 book ai didi

f# - Action 和 unit -> F# 中的 unit 类型之间的重载解析不明确

转载 作者:行者123 更新时间:2023-12-05 01:29:15 25 4
gpt4 key购买 nike

给定以下片段:

type Foo() =
static member Test (act : unit -> unit) = act()
static member Test (act : Action) = Foo.Test act.Invoke

我在最后一行出现错误,说明:无法根据此程序点之前的类型信息确定方法“测试”的唯一重载。可能需要类型注释。

不幸的是,类型注释 (act.Invoke : unit -> unit)不能解决歧义,我找不到可以修复它的注释。我想要 Action成为 -> 的包装器的版本版本。我的特定用例是定义一个将从 F# 和 C# 调用的类,因此我希望它能够在两种语言中本地工作。

最佳答案

F# 将转换插入到 Action对你来说,所以你可以(理论上)用一种方法来解决问题。

type Foo() =
static member Test (act : Action) = act.Invoke()

Foo.Test (fun () -> ())

我不知道如何强制编译器在 Action 之间进行选择和 unit -> unit .另外两个选项:
  • 制作 Action重载初级并执行Foo.Test(Action(act))
  • 将逻辑放在从公共(public)方法
  • 调用的第三个函数中

    但我的建议是采用 Action 的单一方法。 .由于转换是自动的,因此互操作在某种意义上是免费的。

    关于f# - Action 和 unit -> F# 中的 unit 类型之间的重载解析不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198597/

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