考慮這個接口:當用作通用接口參數時,爲什麼`unit`被F#類型系統區別對待?
type A<'a> =
abstract X : 'a
讓我們試着去實現它與int
作爲一般的說法:
{ new A<int> with member this.X = 5 } // all is well
現在,讓我們試着unit
一個說法:
// Compiler error: The member 'get_X : unit -> unit' does not have the correct type to override the corresponding abstract method.
{ new A<unit> with member this.X =() }
現在,如果我們定義一個非通用接口,一切也都很好:
type A_int =
abstract X : int
{ new A_int with member this.X = 5 } // works
type A_unit =
abstract X : unit
{ new A_unit with member this.X =() } // works as well!
有什麼我可以解決這個問題嗎?
在C#中不可能有一個返回'void'的泛型函數 - 例如參見https://programmers.stackexchange.com/questions/131036/why-is-void-not-allowed-as-a- generic-type-in-c。在這種情況下,類似的限制可能被應用於F#代碼。 – 2014-10-10 11:40:59