返回类型为'x = unit的方法不能覆盖泛型抽象方法

时间:2016-06-20 14:10:59

标签: generics f# override 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() = () 。有人能说出原因吗?

// Property case.
type [<AbstractClass>] BaseClass2<'x>() =
  abstract member P: 'x

type UnitClass2() =
  inherit BaseClass2<unit>()

  // The same error as the above.
  member this.P = ()

此问题也发生在属性上。

type [<AbstractClass>] BaseClass3<'x>() =
  abstract member F: 'x -> unit 

type UnitClass3() =
  inherit BaseClass3<unit>()

  // OK
  override this.F(()) = ()

但是,如果我将泛型参数移动到参数侧,那么该代码将编译。

'x

原因是,如果unit不是type [<AbstractClass>] BaseClass4<'x>() = abstract member P: 'x type UnitClass4() = inherit BaseClass4<int>() // OK override this.P = 0 ,则会进行编译。

Array.forEach

环境:Windows 10,F#4.0,Visual Studio 2015社区

0 个答案:

没有答案