In the book 7 Languages in 7 Weeks there is a question:
How would you change / to return 0 if the denominator is zero?
Thanks to the thread What's the significance of self inside of a method? I have a working solution, but I wanted to try to do it without clobbering the Number "/" method, and instead subclass Number. Here is what I tried:
Zeroable := Number clone
Zeroable / = method(denom, if(denom == 0, 0, self proto / denom))
However, this doesn't work. If I try to instantiate an instance of Zeroable, it behaves just like a number:
Io> ten := Zeroable 10
==> 10
Io> ten type
==> Number
Io> ten / 5
==> 2
Io> ten / 0
==> inf
Io> ten slotNames
==> list()
If I instantiate the Zeroable the "normal" way it works, but the value is always 0 and there doesn't appear to be a way to change it:
Io> zero := Zeroable clone
==> 0
Io> zero type
==> Zeroable
Io> zero / 0
==> 0
Io> zero / 2
==> 0
I think the issue is the way that ten
is instantiated, but I cannot figure out how to pass "arguments" to a clone method, or otherwise how to create a Zeroable that is not 0. What is going on here?