[英]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.