2012-09-28 153 views
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

使用#语法来访问类型A似乎工作:

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

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