While playing around with the Array
class in GNU Smalltalk, I was suprised that the #at:put:
message belongs to the class SmallInteger
instead of the class Array
(or any other super class of Array
). Why?
#at:put:
is define on Object
. SmallInteger
does redefine it to throw an exception since it is not indexable and any subclass while also not be indexable.
Also this is a bit tricky.
Long explanation:
1) SmallInteger
are just translate into integer in the VM.
In fact the SmallInteger
a
is translate into (a bitShift: 1) bitOr: 1
in the VM.
2) #at:put:
is a primitive that check if the class is indexable. Indexable object are the one that can be sent #at:put:
, #at:
and have to be instantiate via #new:
. This is the way to have variable instances and it is the only way. (Tangent: OrderedCollection
is instantiating several indexable object in other to let you call #add:
multiple time and feel like the length was infinite).
3) Knowing that if #at:put:
was not redefine in the SmallIntager
class, the primitive would have to first check if the object is an SmallInteger
then check if it is indexable. This would cause you some performance penalty. And redefining the #at:put:
on SmallInteger
just remove the SmallInteger
check.