[Newbies] Re: Adding methods to Integers...
bert at freudenbergs.de
Thu Apr 12 16:31:55 UTC 2007
This is actually wrong. Only SmallIntegers are special [*].
What happens is this: When you add two SmallIntegers (like "3 + 4"),
and the result is a SmallInteger, the result is calculated in the
bytecode directly. Otherwise, a regular send of #+ is performed. From
there, everything else happens in the image, including conversion to
LargeIntegers - see implementors of #+. Like, if the receiver was a
SmallInteger, it tries the "add" primitive, which fails, and then the
implementation of #+ in class Integer is invoked. This method then
creates a large integer.
For the implementation of all this see #bytecodePrimAdd and
#primitiveAdd. You may have to load VMMaker first.
- Bert -
[*] Well, Floats are optimized a bit, and we have a plugin to speed
up LargeIntegers, but this is all optional and doesn't matter in
On Apr 12, 2007, at 18:05 , Klaus D. Witzel wrote:
> Hi Patrick,
> some of the symptoms you describe have to do with a small set of
> classes being "hardwired" in Squeak's virtual machine. To see which
> they are, evaluate (printIt)
> Smalltalk specialObjectsArray select: [:each | each isBehavior]
> So when you do primitive arithmethic with your own subclass of
> LargePositiveInteger, the VM returns an instance of
> LargePositiveInteger (and not your subinstance of it).
> Of course the specialObjectsArray can be changed and from then on
> the VM (after being notified) will use your subclass but, I think
> this is not what you really want ;-)
> Putting your methods into Integer is fine as long as they do not
> conflict with anything else. Yes, this is the usual approach for
> adding new behavior to all the integers :)
> On Thu, 12 Apr 2007 17:28:01 +0200, you wrote:
>> The most recent MathFactor Podcast ( http://mathfactor.uark.edu/ )
>> ended with a request to write a computer program that could, in
>> principal, given enough time and memory, compute Graham's Number
>> ( http://mathworld.wolfram.com/GrahamsNumber.html ).
>> Smalltalk was a natural choice since it already supports
>> Being new to Smalltalk, my first thought was that I should make my
>> own class as a subclass of LargePositiveInteger, put my methods
>> and violá. Alas, no love.
>> I ran into the following problems:
>> * I couldn't find a way to give a value to myself.
>> * I couldn't find a way to get integers to adapt to my class
>> I ended up just adding my methods to the Integer class. But,
>> this felt
>> very naughty. Is it the usual approach?
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
More information about the Beginners