Can operators in Smalltalk be overloaded?

前端 未结 5 755
你的背包
你的背包 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:17

    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.

提交回复
热议问题