[英]How to implement an F# interface with a member returing an instance of that interface?
[英]How to implement an interface member that returns void in F#
想象一下C#中的以下界面:
interface IFoo {
void Bar();
}
我怎样才能在F#中实现它? 我在网上搜索30分钟时发现的所有示例都只显示了返回类型的示例,我认为这些示例在功能样式中更常见,但在这种情况下我无法避免。
这是我到目前为止所拥有的:
type Bar() =
interface IFoo with
member this.Bar() =
void
_FS0010失败:
expression_中出现意外的关键字'void'。
等价的单位在语法上定义为()
。
type Bar() =
interface IFoo with
member this.Bar () = ()
有关F#类型的一般信息,请参阅
从该页面:
单位类型只有一个值,写成“()”。 它有点像“void”,在某种意义上说,如果你有一个只调用副作用的函数(例如printf),这样的函数将具有返回类型“unit”。 每个函数都接受一个参数并返回一个结果,因此你使用“unit”来表示参数/结果是无趣/无意义的。
返回类型需要是(),所以像成员this.Bar =()这样的东西应该做
F#中的等价物是:
type IFoo =
abstract member Bar: unit -> unit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.