Alex Rivera | Logout

How do you replace existing operators without invoking them in Io?

Asked 2010-11-23T23:43:06.850
32

I’m trying to complete the second exercise on IO day 2 in the book Seven Languages in Seven Days. In it your asked, “How would you change / to return 0 if the denominator is zero?” I've determined that I can add a method to Number using:

Number new_div := method(i, if(i != 0, self / i, 0))

What I’m not sure is how to replace the ”/” in the operator table. I’ve tried:

Number / := Number new_div
Number / := self new_div

But I get an exception to both as it’s trying to invoke ”/”. How do I get a handle on Number / so I can store a reference to the old method and then redefine it for my own purposes? Am I going about this all wrong?

Edit
Report

1 Answer

4

What if you used the power operator inside your redefinition, then you don't have to keep a reference to the old division operator.

Number / := method(i, if(i==0, 0, self*i**(-1)))
answered 2011-04-08T01:43:33.097

Your Answer