[Newbies] Re: Adding methods to Integers...

Klaus D. Witzel klaus.witzel at cobss.com
Thu Apr 12 16:05:13 UTC 2007


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 :)

/Klaus

On Thu, 12 Apr 2007 17:28:01 +0200, you wrote:

>
> Background:
> ::::::::::::::::::::::
>
> 	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 LargeIntegers.
>
> Problem:
> :::::::::::::::
>
> 	Being new to Smalltalk, my first thought was that I should make my
> 	own class as a subclass of LargePositiveInteger, put my methods there,
> 	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 anyway.
>
> 	I ended up just adding my methods to the Integer class.  But, this felt
> 	very naughty.  Is it the usual approach?
>
> Thanks,
> Patrick



More information about the Beginners mailing list