Default Class Extension (SmallInteger)

I am trying to extend the SmallInteger class with a new square method. The idea is that I want to be able to call "5 squares" and it will return 25.

Extending your own classes using instance methods is quite simple since you know the names of the variables, but I don't know the names of the variables in the SmallInteger class. How to find them?

I think it should look something like this, but "thisNumber" refers to any number of this SmallInteger object.

SmallInteger extend [
    square [
        | r |
        r := thisNumber * thisNumber.
        ^r
    ]
]
+4
source share
1 answer

GNU-Smalltalk, , , Smalltalk - self.

square
  ^self * self

SmallInteger voilà.

, , , . squared ( $d .) , square, Smalltalk .

, squared SmallInteger, Number. , , , ( Fraction .)

, self * self SmallInteger, a LargePositiveInteger. , Smalltalk , Number, - .

+7

All Articles