Can operators in Smalltalk be overloaded?

前端 未结 5 716
你的背包
你的背包 2021-01-02 16:23

Is it possible to overload operators in Smalltalk?

I am looking for tutorials/examples.

Thanks.

5条回答
  •  借酒劲吻你
    2021-01-02 17:09

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

提交回复
热议问题