冲突的嵌套遗传特征
假设我有以下代码:
trait Trait1 {
trait Inner {
val name = "Inner1"
}
}
trait Trait2 {
trait Inner {
val name = "Inner2"
}
}
class Foo extends Trait1 with Trait2 {
// I want Concrete1 to be a Trait1.Inner not a Trait2.Inner
class Concrete1 extends Inner
val c = new Concrete1
}
object Obj {
def main(args: Array[String]): Unit = {
val foo = new Foo
println(foo.c.name)
}
}
当我混合 Trait1
和 Trait2
时,引用 Inner
似乎默认为 Inner
code> 我第二个混合的特征的类型;因此,当我调用 Obj
的 main
方法时,它会打印 Inner2
。如何在 Foo
中引用 Trait1.Inner
?以下所有三个都会给出编译器错误:
class Concrete1 extends Trait1.Inner
class Concrete1 extends Trait1$Inner
class Concrete1 extends Trait1#Inner
Suppose I have the following code:
trait Trait1 {
trait Inner {
val name = "Inner1"
}
}
trait Trait2 {
trait Inner {
val name = "Inner2"
}
}
class Foo extends Trait1 with Trait2 {
// I want Concrete1 to be a Trait1.Inner not a Trait2.Inner
class Concrete1 extends Inner
val c = new Concrete1
}
object Obj {
def main(args: Array[String]): Unit = {
val foo = new Foo
println(foo.c.name)
}
}
When I mix in Trait1
and Trait2
, referring to Inner
seems to default to the Inner
type of whichever trait I mixin second; so when I call Obj
's main
method it prints Inner2
. How can I refer to Trait1.Inner
in Foo
? All three of the following give compiler errors:
class Concrete1 extends Trait1.Inner
class Concrete1 extends Trait1$Inner
class Concrete1 extends Trait1#Inner
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是
使用这个,
这应该能让你得到你想要的
Instead of
Use this
That should get you what you want
模板中有两个命名空间(模板是类、对象或特征的主体。)
当从多个父模板继承时,这些命名空间中的冲突通过类线性化来解决。
您可以重新排序您的继承,以将所需的父 Inner 引入您的类,或者找到替代设计。
There are two namespaces within a template (template being the body of a class, object, or trait.)
When inheriting from multiple parent templates, conflicts in these namespaces are resolved through class linearization.
You could re-order your inheritance to bring the desired parent Inner into your class, or find an alternative design.
一种选择(如果您可以侵入特征)是将每个内部特征定义为具有不冲突名称的类型成员。
如果无法侵入原始特征(Trait1 和 Trait2),则可以扩展它们来定义类型成员。
另一种方法是使用中间特征来定义您的第一个具体类:
One option (if you can be invasive to the traits) is to define each Inner trait as a type member that has a non-conflicting name.
If you cannot be invasive to the original traits (Trait1 and Trait2), you can extend them to define the type member.
Another approach would be to use an intermediate trait to define your first concrete class:
为什么不按照您期望的优先顺序对特征进行排序呢?特征的线性化不是任意的,而是指定的。
Why not order the traits in the order you expect them to have precedence? The linearization of traits is not arbitrary, but specified.