2013-05-04 94 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] = ???