F# 通行证;单位「;作为F中泛型类的类型参数#

F# 通行证;单位「;作为F中泛型类的类型参数#,f#,F#,我有一个接口,它接受一个泛型参数,并有一个抽象方法 type MyInterface<'a> = abstract member abstractMethod: 'a -> 'a 键入MyInterface'a …我有一个派生类,它使用类型参数unit从基类继承 type Derived() = interface MyInterface<unit> with override this.abstractMethod (_) =

我有一个接口,它接受一个泛型参数,并有一个抽象方法

type MyInterface<'a> =
    abstract member abstractMethod: 'a -> 'a
键入MyInterface'a
…我有一个派生类,它使用类型参数unit从基类继承

type Derived() =
    interface MyInterface<unit> with
        override this.abstractMethod (_) = ()
类型派生()=
接口MyInterface与
重写此.abstractMethod())=()
但是编译器抱怨说

错误成员“abstractMethod:unit->unit”没有 请更正类型以重写相应的抽象方法

如果我使用另一种类型而不是unit,例如,int,代码将编译

这是编译器中的错误吗?有解决办法吗

谢谢

单元
在互操作方面是“特殊的”:当函数采用
单元
作为参数时,它作为无参数函数编译为IL,当函数返回
单元
作为结果时,它被编译为
无效
函数。由于这种欺骗,您不能在互操作设置(例如类和接口)中将
unit
真正用作“通用”类型

事实上,当我试图在我的机器上编译您的代码时,我得到了一个不同的错误:

The member 'abstractMethod : unit -> unit' is specialized with 'unit' 
but 'unit' can't be used as return type of an abstract method
parameterized on return type.
也就是说,大概是我上面所说的


(我不知道你为什么得到你得到的东西;也许你使用的是F#的旧版本)

你对
抽象方法
的重写没有签名
unit->unit
——它需要一个未指定类型的参数(名为
)。