Scala将递归有界类型参数(F-bounded)转换为类型成员

Ale*_*rlo 11 types scala parametric-polymorphism type-members bounded-quantification

我将如何转换:

trait Foo[A <: Foo[A]]
Run Code Online (Sandbox Code Playgroud)

一个类型的成员?

也就是说,我想要的东西如下:

trait Foo {
  type A <: Foo {type A = ???}
}
Run Code Online (Sandbox Code Playgroud)

但我遇到了困难,因为名称A已经在类型细化中被采用.这个问题类似(并衍生自):通过类型成员而不是类型参数进行F限制量化?

kir*_*uku 16

使用自我类型:

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 }
                                    ^
Run Code Online (Sandbox Code Playgroud)