I was looking through the internals of Pharo and noticed that the definition of arithmetic + and - look very much alike:
+ aNumber
"Refer to the comment in Number + "
aNumber isInteger ifTrue:
[self negative == aNumber negative
ifTrue: [^ (self digitAdd: aNumber) normalize]
ifFalse: [^ self digitSubtract: aNumber]].
aNumber isFraction ifTrue:
[^Fraction numerator: self * aNumber denominator + aNumber numerator denominator: aNumber denominator].
^ aNumber adaptToInteger: self andSend: #+
and
- aNumber
"Refer to the comment in Number - "
aNumber isInteger ifTrue:
[self negative == aNumber negative
ifTrue: [^ self digitSubtract: aNumber]
ifFalse: [^ (self digitAdd: aNumber) normalize]].
aNumber isFraction ifTrue:
[^Fraction numerator: self * aNumber denominator - aNumber numerator denominator: aNumber denominator].
^ aNumber adaptToInteger: self andSend: #-
As I see it, this is completely against the OO way of designing things and is generally bad. Why doesn't anybody find a better solution?
The simplest thing I can think of is:
- aNumber
^self + aNumber negated
However, this will have a cost:
What we see here is a tribute to optimization. Not premature optimization, this is a low level operation used extensively.
There are other things in this code which are not perfect:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With