Hello there!
I’ve been following the Writing Classes help file but I got it all wrong. My classes init
is called several times (once for every custom parent class) ! This works but, hum…
This is because I’ve been stupidly copy-pasting this everywhere:
*new { |parent, bounds|
^super.new.init;
}
There’s this warning on the doc:
If the superclass also happened to call super.new.init it will have expected to call the .init method defined in that class (the superclass), but instead the message .init will find the implementation of the class that the object actually is, which is our new subclass. In such cases, a unique method name like myclassInit should be used.
But I hope there’s more than that?
Now let’s say I have 1 000 classes, with class 1 being parent of class 2, class 2 parent of class 3, etc…
I think they all implement:
*new { |parent, bounds|
^super.new;
}
Called from class 1000, this goes bottom-up, up until Object.new
is called. Now, I’d like to init
every class top-down. Is this possible:
*new { |parent, bounds|
var me = super.new;
me.init;
^me
}
init {
do something specific to this class
}
If me.init;
always look at class 1000’s init
method, then we’re screwed right ? It means every class must have a unique init method name ? Can ^super.init
be helpful here ?
As a side question, in terms of syntax, are we removing the ;
character from return statements within .sc
files:
*new { |parent, bounds|
^super.new
}
Thanks for your help!