我可以使用别名以便在以下名称冲突情况下不需要更改任何类型参数/成员吗:
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 }]
}
但我真的不想更改类型名称。
最佳答案
例如,您可以做这样的事情:
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] = ???
关于scala - 类型别名以避免类型细化中的名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375857/