Can operators in Smalltalk be overloaded?

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...


For most part, things that are operators in other languages are in Smalltalk unary or binary messages like +, *, /, ... etc. Classes are free to respond to those messages as they seem fit, so yes, you can redefine behavior of +, and you can also make instances of some non number classes understand and respond to it.

For instance look at the implementation of + in Point class.

One thing to note, := and ^ are not messages, so they can not redefined in a way described above.

Btw, for learning Smalltalk, one of the greatest resources of examples and code is the Smalltalk image. So I recommend that you fire up Smalltalk, and learn your way to browse through vast amount of examples that it contains.


There's no operators in smalltalk, except assignment. Everything is implemented in classes as methods. So if you want to change behaviour of = or + / - methods, just look at their implementors. Or if you want to make instances of your class to understand those messages, just implement them.