Search code examples
operator-overloadingsmalltalk

Can operators in Smalltalk be overloaded?


Is it possible to overload operators in Smalltalk?

I am looking for tutorials/examples.

Thanks.


Solution

  • Method overloading is not possible in Smalltalk. Instead, a combination of method overriding and a technique called double dispatch is used to implement the same behavior as operator overloading in other languages.

    You can find an example implementation in the mathematical operators +,*,/,- (which are binary messages in Smalltalk). Here is the idea: the implementation of Integer>>+ sends a message #addWithInteger: to its argument. The implementation of #addWithInteger: is implemented on each Magnitude subclass, such as to specialize addition of Int+Int, Float+Int, etc...