2012-09-28 135 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的成員不在自我類型聲明的範圍內,因此您必須明確引用該類型。