0

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?

Marc
  • 585
  • 5
  • 19

1 Answers1

4

#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.

mathk
  • 7,973
  • 6
  • 45
  • 74
  • Thany you for the enlightenment :-) But when #at:put: is defined in the class Object, means this that I have to redefine this message in every not-indexable class? – Marc Jun 16 '11 at 13:04
  • Nop only for does that are handle in a different way by the VM. Which will happen only if you decide to hack in the VM. – mathk Jun 17 '11 at 07:59