At the intersection of Cool and Smalltalk and Useless
Monday, January 22nd, 2007In Smalltalk, you when an object receives a message that it does not implement, it executes the #doesNotUnderstand: method (which has a default implementation in the Object class). Now, if you override this method in your class, you can do funny stuff like proxies and even more useless things, like the one I will present here :)
The idea that came thourgh my head today and I implemented together with Paolo (not because it was that difficult but because it was that fun) was to override Number>>doesNotUnderstand: such that if the method that is being sent to an object of class Number looks like an attempt to convert something to something else… use Google to convert the thing and return the result. After the task was done, the following expressions suddenly became valid Smalltalk expressions:
1 usdToEur
37 celsiusToFarenheit
1 kmToMile
If you want to see the implementation (although many other better things you probably have to do with your life) check out the Conversions-PoweredByGoogle in the Cincom Public Repository.
There are many lessons to be learned from this experiment. I will however, not mention any. Lessons are to be learnt rather than taught…
