interface - 如何在 F# 中实现返回 void 的接口(interface)成员

标签 interface compiler-errors f# void unit-type

想象一下 C# 中的以下接口(interface):

interface IFoo {
    void Bar();
}

如何在 F# 中实现这一点?我在 30 分钟的在线搜索中找到的所有示例仅显示具有返回类型的示例,我认为这些示例在函数式样式中更为常见,但在这种情况下我无法避免。

这是我到目前为止所拥有的:
type Bar() =
    interface IFoo with
        member this.Bar() =
            void

_FS0010 失败:

Unexpected keyword 'void' in expression_.

最佳答案

等效的单位是在语法上定义为 () .

type Bar() =
    interface IFoo with
        member this.Bar () = ()

关于interface - 如何在 F# 中实现返回 void 的接口(interface)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3067605/

相关文章:

f# - 如何使用其AST生成F#代码?

objective-c - 各种@interface 声明,有些带有括号

具有名为 "new"的函数的 Typescript 接口(interface)

c++ - 获取编译错误 : 0x2 trying to open file <vfdmsg>

c - 如何使这个 1996 年的 makefile 起作用?

c# - 所有路径都不返回值

f# - 无法从 C# 类使用 F# 中的某些方法

f# - Fparsec v. 0.9.2(新手)中的空白在哪里?

go - 为什么 Golang 需要接口(interface)?

swift - 有没有办法在 Kotlin 中完成 Swift 的协议(protocol)组合