考虑这个界面:
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)
有什么办法可以解决这个问题吗?
在F#中,具有声明的返回类型的抽象槽unit在.NET IL中编译为返回类型void.相反,声明返回类型为"T"的抽象槽在.NET IL中编译为通用返回类型"T",当T通过unit变量实例化时unit'.
请参阅:由于单元导致F#接口继承失败
| 归档时间: |
|
| 查看次数: |
247 次 |
| 最近记录: |