I myself will answer this, despite the fact that all other answers are technically correct. This is because I knew about metaclasses, and it seems that I have the correct super
semantics in my head, but I was still getting unexpected results.
It turns out that I misunderstood the basics of inheritance in Smalltalk and how the method is called.
I thought for such code ...
Object subclass:
... the expression B new runTest
is evaluated as 'A'
- this means that the method from the superclass is evaluated in the enlarged object.
But this is not so.
It is evaluated as 'B'
because there is no promotion, and when we call any method inside the superclass method, the search starts in the real class of the object, and not in the class from which the evaluation method is derived.
As a result, calling ^self new
and ^super new
, although not defining a new
in any of the classes, has the same effect, since both of them end with a call to the implementation of new
Behavior in the context of self
.
source share