vai*_*in0 5 generics f# overriding abstract
在下面的代码中,UnitClass1.F似乎覆盖BaseClass1<unit>.F,但实际上没有.有人能说出原因吗?
// Method case.
type [<AbstractClass>] BaseClass1<'x>() =
abstract member F: unit -> 'x
type UnitClass1() =
inherit BaseClass1<unit>()
// ERROR: F: unit -> unit doesn't have the correct type to override the corresponding abstract method.
override this.F() = ()
// ERROR: Wrong number of parameters.
//override this.F(()) = ()
// ERROR: UnitClass1 doesn't have an implementation for abstract member F: unit -> 'x
//member this.F() = ()
Run Code Online (Sandbox Code Playgroud)
属性上也会出现此问题.
// Property case.
type [<AbstractClass>] BaseClass2<'x>() =
abstract member P: 'x
type UnitClass2() =
inherit BaseClass2<unit>()
// The same error as the above.
member this.P = ()
Run Code Online (Sandbox Code Playgroud)
但是,如果我将泛型参数移动到参数侧,那么该代码将编译.
type [<AbstractClass>] BaseClass3<'x>() =
abstract member F: 'x -> unit
type UnitClass3() =
inherit BaseClass3<unit>()
// OK
override this.F(()) = ()
Run Code Online (Sandbox Code Playgroud)
当然,如果'x不是,他们会编译unit.
type [<AbstractClass>] BaseClass4<'x>() =
abstract member P: 'x
type UnitClass4() =
inherit BaseClass4<int>()
// OK
override this.P = 0
Run Code Online (Sandbox Code Playgroud)
环境:Windows 10,F#4.0,Visual Studio 2015社区