Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `super` statically bound in classes?

I am reading the chapter about traits in "Programming in Scala" by Martin Odersky et al (2ed) and I am puzzled by a statement that super in class in statically bound, unlike in trait, where it is dynamically bound (p.220).

I understand this statement, but when it comes to example like this one:

val queue = (new BasicIntQueue with Incrementing with Filtering)

on p.229 or entire explanation of linearization (p.234) it seems to me, that super cannot be statically bound, because otherwise stacking traits would not be possible -- i.e. when class "starts" the chain of calling of stacked method with super already resolved, it would hit direct class parent no matter what user added to the stack of traits.

What am I missing? :-) Is super really statically bound to its parent?

like image 335
greenoldman Avatar asked Jun 20 '26 04:06

greenoldman


1 Answers

val queue = (new BasicIntQueue with Incrementing with Filtering)

on p.229 or entire explanation of linearization (p.234) it seems to me, that super cannot be statically bound, because otherwise stacking traits would not be possible -- i.e. when class "starts" the chain of calling of stacked method with super already resolved, it would hit direct class parent no matter what user added to the stack of traits

But the chain in this case doesn't start with BasicIntQueue's methods, but with Filtering's. It only starts with the class if no mixed-in traits override the 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 refer to BasicIntQueue with Incrementing with Filtering, and this is resolved statically.

However, super calls in Incrementing are resolved dynamically: in BasicIntQueue with Incrementing they will refer to BasicIntQueue, while in BasicIntQueue with Filtering with Incrementing they will refer to BasicIntQueue with Filtering, without any changes in Incrementing.

like image 123
Alexey Romanov Avatar answered Jun 23 '26 09:06

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!