Scala 对traits中的超类型方法的调用允许使用'extends',但不能使用self类型

Scala 对traits中的超类型方法的调用允许使用'extends',但不能使用self类型,scala,inheritance,traits,self-type,Scala,Inheritance,Traits,Self Type,考虑: trait SuperBar { def superBarMethod = ??? } trait Bar extends SuperBar trait FooWithSelfType { this: Bar => super.superBarMethod // error: value superBarMethod is not a member of AnyRef } trait FooWithExtends extends Bar { super.superBar

考虑:

trait SuperBar { def superBarMethod = ??? }
trait Bar extends SuperBar

trait FooWithSelfType { this: Bar =>
  super.superBarMethod // error: value superBarMethod is not a member of AnyRef
}

trait FooWithExtends extends Bar {
  super.superBarMethod
}
这种限制是由于一些潜在的实现缺陷造成的,还是它实际上以这种方式设计是有原因的


我的看法是,如果
this
已知为
Bar
类型,并且
Bar
已知为
SuperBar
的子类型,那么应该允许调用
this
上的任何
SuperBar
方法。

FooWithSelfType
可能知道它是
Bar
,但是它实际上并不是
FooWithSelfType
继承层次结构的一部分,因此它不能访问
super
,除非
super
是其继承层次结构的一部分。如果你有

trait Baz extends SuperBaz { this : Bar => 
  /* ... */
}

如果
SuperBaz
SuperBar
都是可能的,你怎么知道
super
指的是什么呢?

自我类型说,当前的特质最多应该混合到你作为自我类型使用的类型中。这意味着给定类型的所有子类型都可以混合当前特征。考虑到这一点,super仅指当前特征是其子类/特征的类/特征


我不得不说,不久前我就遇到了这种想法,我认为这可能是一个弱点,我的意思是我们可以想象编译器可以在超类和self类型的超类中查找这两种方法。

为什么需要使用
super来限定对
superBarMethod
的调用。
。它已经可以使用,而不需要使用
super.
正如您声明自己需要
Bar
可用一样。那么,就不能问同样的“您怎么知道…”问题,关于
trait Baz使用Bar{super.superBarMethod}
扩展SuperBaz吗?这完全是一种法律解释。。。换句话说:如果
super
extends SuperBaz with Bar
的情况下工作,那么它也可以在
extends SuperBaz的情况下工作{this:Bar=>
,在我看来…不确定编译器需要付出多少努力。P.S.毫无疑问,对于编译器目前拒绝接受这一点,有一个技术解释,但我想知道是否有一个更根本的原因,编译器不能(被迫)按“预期”处理这一问题@ErikAllik-有线性化规则,你可以通过对特征列表进行重新排序来获得你想要的线性化。但是,你不能根据自我类型注释对这些特征进行重新排序。更不用说线性化已经够复杂了。好吧,尽管我还不能完全理解,但我相信你的能力e、 @ErikAllik-简而言之,
class-Qux扩展了Foo,其中Bar和Baz
是它的直接超类型。
class-Qux扩展了Baz,Foo和Bar是它的直接超类型。这已经足够灵活了,不必担心自身类型如何交互。换句话说,我认为这是这不是数学上的限制,而是如何定义超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超超。