Is `super` statically linked in classes?
I am reading the chapter on traits in Programming in Scala by Martin Oderski et al. (2ed) and I am puzzled by the statement that super
a class is statically linked, as opposed to a trait where it is dynamically linked (p. 220).
I understand this statement, but when it comes to an example like this:
val queue = (new BasicIntQueue with Incrementing with Filtering)
on page 229 or a complete explanation of linearization (page 234) it seems to me that super
it is impossible to statically link, because otherwise stacking may not be possible, i.e. when the class "starts" the chaining, the call to the stacked method with is super
already resolved, it will go straight to the parent class, no matter which user added the traits to the stack.
What am I missing? :-) Is it super
really statically linked to its parent?
source to share
val queue = (new BasicIntQueue with Incrementing with Filtering)
on page 229 or a complete explanation of linearization (page 234) it seems to me that super cannot be statically linked, otherwise stacking may not be possible, i.e. when a class "starts" a call chain of a complex method with a super already resolved, it will go straight to the parent class, no matter which user added to the traits stack
But the chain in this case does not start with methods BasicIntQueue
, but with Filtering
. It only starts with a class, unless mixed traits override this method. If you define instead
class MyQueue extends BasicIntQueue with Incrementing with Filtering {
.. // some super calls
}
or
// an anonymous class
val queue = new BasicIntQueue with Incrementing with Filtering {
.. // some super calls
}
super
will link to BasicIntQueue with Incrementing with Filtering
and it will be resolved statically.
However, super
calls are Incrementing
resolved dynamically: in BasicIntQueue with Incrementing
they will refer to BasicIntQueue
, and in BasicIntQueue with Filtering with Incrementing
they will refer to BasicIntQueue with Filtering
, without any changes in Incrementing
.
source to share