2013-05-04 91 views
3

我可以使用別名,所以我不需要改變任何類型的參數/成員的以下名稱衝突的情況:型走樣,以避免在類型精名稱衝突

trait Bar { 
    type A 
} 

trait Foo { 
    def get[A]: Option[Bar { type A = A }] // "illegal cyclic reference" 
} 

我知道我可以寫

trait Foo { 
    def get[A1]: Option[Bar { type A = A1 }] 
} 

但我真的不希望改變類型名稱。

回答

3

你可以例如做這樣的事情:

trait Bar { 
    type A 
} 

trait Foo { 
    type M[X] = Bar { type A = X } 
    def get[A]: Option[M[A]] 
} 

或內聯:

def get[A]: Option[({ type X[Y] = Bar { type A = Y }})#X[A]] = ??? 

或者,如果你喜歡:

def get[A]: Option[({ type A1 = A; type X = Bar { type A = A1 }})#X] = ???