logo       

Adding methods to Integers...: msg#00090

lang.smalltalk.squeak.beginners

Subject: Adding methods to Integers...


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



Bonus Questions:
:::::::::::::::::::::::::::::

[ Actually, this section should be titled, "Bonus Questions: which it turns
out that I managed to answer for myself, but have left here for pedagogic
reasons". ]

What I really want at the moment is to know if adding methods to the
Kernel classes is considered legit. But, I ran into a whole set of other
questions along the way. So, I wouldn't mind a little guidance on these
either.

The new: method:
::::::::::::::::::::::::::::::

The withAtLeastNDigits method of LargePositiveInteger does this
(variable names changed for clarity):

nn := self class new: nDigits.

I cannot find the "new:" method in the class browser under Class,
nor can I find any hint of it somehow being part of LargePositiveInteger,
Integer, Number, Magnitude, Object, or ProtoObject. Actually, I just
answered this one myself.... the Class class's hierarchy is
Class < ClassDescription < Behavior < Object. The "new:" is in Behavior.

Initialization methods?:
:::::::::::::::::::::::::::::::::::::::

The only way that I can understand the "i" and "hours" methods of the
Number class are if I somehow think the Complex and Duration classes
have class methods in addition to their instance methods. For example,
here is the "i" method of the Number class.

i
^ Complex real: 0 imaginary: self

That seems straightforward enough. Given a number, say "5" and pass
it the "i" message. It will return a complex number with 0 for the real
and 5 for the imaginary. Except, I can't where the "real: imaginary:"
method is defined with the class browser.

Blah, and I just answered this one, too. I hadn't twigged on the "class"
button at the bottom of the class-list in the browser. Now, I see.

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise