当用作通用接口参数时,为什么`unit`被F#类型系统区别对待?

Mis*_*hor 7 f# unit-type

考虑这个界面:

type A<'a> =
    abstract X : 'a
Run Code Online (Sandbox Code Playgroud)

让我们尝试用它int作为通用参数来实现它:

{ new A<int> with member this.X = 5 } // all is well
Run Code Online (Sandbox Code Playgroud)

现在,让我们尝试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 = () }
Run Code Online (Sandbox Code Playgroud)

现在,如果我们定义一个非泛型接口,一切都运行良好:

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!
Run Code Online (Sandbox Code Playgroud)

有什么办法可以解决这个问题吗?

Art*_*CLI 5

在F#中,具有声明的返回类型的抽象槽unit在.NET IL中编译为返回类型void.相反,声明返回类型为"T"的抽象槽在.NET IL中编译为通用返回类型"T",当T通过unit变量实例化时unit'.

请参阅:由于单元导致F#接口继承失败