自我类型无法访问类型成员

时间:2012-09-28 15:32:49

标签: scala abstract-type self-type

给定具有类型参数的特征,以及具有抽象类型成员的特征:

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
}

1 个答案:

答案 0 :(得分:4)

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

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

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