小编vai*_*in0的帖子

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

在下面的代码中,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>() = …
Run Code Online (Sandbox Code Playgroud)

generics f# overriding abstract

5
推荐指数
0
解决办法
62
查看次数

如何使用重写的抽象成员的基本实现?

有一个接口(比如IA),一个接口IA的实现(比如Base),以及一个派生类Base(比如Derived),它覆盖了IA的抽象成员.现在,在重写成员的实现中,我想使用Base的成员的实现.但是,我不知道怎么写这样做.

这段代码说明了我的问题:

type Flag =
  | F1 = 1
  | F2 = 2

type IA =
  abstract member A: Flag

type Base() =
  interface IA with
    member this.A = F1

type Derived() =
  inherit Base()

  interface IA with
    override this.A = (* ? *)
      // base.A ||| F2             // NG (Compile error that `A` isn't a member of `base`)
      // (base :> IA).A ||| F2     // NG (Syntax error)
      // (this :> IA).A ||| F2     // NG (Infinite recursion)
Run Code Online (Sandbox Code Playgroud)

f# overriding member abstract

4
推荐指数
1
解决办法
368
查看次数

标签 统计

abstract ×2

f# ×2

overriding ×2

generics ×1

member ×1