11

我将如何转换:

trait Foo[A <: Foo[A]]

类型成员?

即,我想要以下内容:

trait Foo {
  type A <: Foo {type A = ???}
}

但我遇到了困难,因为名称 A 已经在类型细化中采用。这个问题是相似的(并且产生自):F-bounded quantification through type member instead of type parameter?

4

1 回答 1

17

使用自我类型:

scala> trait Foo { self => type A <: Foo {type A = self.A}}
defined trait Foo

scala> class Bar extends Foo { type A = Bar }
defined class Bar

scala> class Bar extends Foo { type A = Int }
<console>:10: error: overriding type A in trait Foo with bounds <: Foo{type A = Bar.this.A};
 type A has incompatible type
       class Bar extends Foo { type A = Int }
                                    ^
于 2013-01-09T19:56:43.107 回答