2

给定一个带有类型参数的特征和一个带有抽象类型成员的特征:

trait Foo[A] {
  def schoko(f: A) : Unit
}
trait Bar {
  type A
  def foo: Foo[A]
}

trait X

trait ConcreteBar extends Bar {
  final type A = X
}

是否有任何更改可以使以下任何一项工作:

trait Mixin extends ConcreteBar {
  _: Foo[A] => // "not found: type A"
  def foo = this
}

trait Mixin[A] extends Bar {
  _: Foo[A] =>
  def foo = this // "found: Mixin[A] with Foo[A]   required: Foo[Mixin.this.A]"
}

trait Mixin[A1] extends ConcreteBar {
  _: Foo[A1] =>
  type A = A1  // "error: overriding type A in trait ConcreteBar, which equals X"
  def foo = this
}
4

1 回答 1

4

使用#访问类型的语法A似乎有效:

trait Mixin extends ConcreteBar {
   _: Foo[ ConcreteBar#A ] =>
   def foo = this
}

似乎 的成员ConcreteBar不在自我类型声明的范围内,因此您必须显式引用该类型。

于 2012-09-28T16:19:17.133 回答