Reputation: 38247
Consider:
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
}
Is this limitation due to some underlying implementation shortcoming, or it's actually designed this way for a reason?
The way I see this is that if this
is known to be of type Bar
, and Bar
is known to be a subtype of SuperBar
, then invoking any SuperBar
methods on this
should be allowed.
Upvotes: 3
Views: 206
Reputation:
Self-type says that the current trait should be mixed into is at most the type you are using as a self-type. Meaning that all subtypes of that given type can mix the current trait. With that in mind, super only refers to the class/trait that the current trait is a sub class/trait of.
I have to say that I ran into this thought not that long ago, and I think this could be a weakness, I mean we could imagine that the compiler could look up both methods in the superclass and those in the self-type's superclass.
Upvotes: 2
Reputation: 167911
FooWithSelfType
might know that it is a Bar
, but it's not actually part of FooWithSelfType
's inheritance hierarchy, so it doesn't have access to super
except for the super
that is explicitly part of its inheritance hierarchy. If you had
trait Baz extends SuperBaz { this : Bar =>
/* ... */
}
how would you know to what super
refers if both SuperBaz
and SuperBar
were possibilities?
Upvotes: 3