简体   繁体   English

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

[英]How to implement an interface member that returns void in F#

Imagine the following interface in C#: 想象一下C#中的以下界面:

interface IFoo {
    void Bar();
}

How can I implement this in F#? 我怎样才能在F#中实现它? All the examples I've found during 30 minutes of searching online show only examples that have return types which I suppose is more common in a functional style, but something I can't avoid in this instance. 我在网上搜索30分钟时发现的所有示例都只显示了返回类型的示例,我认为这些示例在功能样式中更常见,但在这种情况下我无法避免。

Here's what I have so far: 这是我到目前为止所拥有的:

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

Fails with _FS0010: _FS0010失败:

Unexpected keyword 'void' in expression_. expression_中出现意外的关键字'void'。

The equivalent is unit which is syntactically defined as () . 等价的单位在语法上定义为()

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

For general info on F# types, see 有关F#类型的一般信息,请参阅

The basic syntax of F# - types F#的基本语法 - 类型

From that page: 从该页面:

The unit type has only one value, written "()". 单位类型只有一个值,写成“()”。 It is a little bit like "void", in the sense that if you have a function that you only call for side-effects (eg printf), such a function will have a return type of "unit". 它有点像“void”,在某种意义上说,如果你有一个只调用副作用的函数(例如printf),这样的函数将具有返回类型“unit”。 Every function takes an argument and returns a result, so you use "unit" to signify that the argument/result is uninteresting/meaningless. 每个函数都接受一个参数并返回一个结果,因此你使用“unit”来表示参数/结果是无趣/无意义的。

返回类型需要是(),所以像成员this.Bar =()这样的东西应该做

The equivalent in F# is: F#中的等价物是:

type IFoo =
    abstract member Bar: unit -> unit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM