Could someone please explain the process of double dispatch in Pharo 4.0 with Smalltalk? I am new to Smalltalk and am having difficulty grasping the concept, since it is implemented very differently in Java as compared to Smalltalk. It will be very helpful if some one could explain it with an example.
相关问题
- Simple class definition error in smalltalk
- Which Smalltalk object should be passed to a C fun
- Squeak(smalltalk) subSrings ignores empty strings
- Smalltalk variables: why should I declare them?
- How to manage 2d array in Smalltalk?
相关文章
- Implementation Strategies for Object Orientation
- What is the difference between Seaside programmmin
- How to copy several Monticello commits from local
- Smalltalk ReferenceStream has problems with new in
- Is it possible to write shell command within Pharo
- Draw table in Pharo
- Draw table in Pharo
- Double dispatch in Pharo
Essentially the idea is that you have method:
#addInteger:
which knows how to add integers,#addFloat:
which knows how to add floats,Now in
Integer
class you define+
as:in
Float
you define it like:This way you only need to send
+
to an object and then it will ask the receiver to add it with the required method.Another strategy is to use
#adaptTo:andSend:
methods. For example+
inPoint
class is defined as:Which first checks if the argument is a Point, and if it's not, asks the argument to adapt to Point and send some symbol (operation), this saves some duplication of the methods that have to perform a slightly different operation.
Collection
implements the method like this:and
Number
implements it like that:Note, that to avoid explicit type checking, we could define that method in the
Point
itself this way:You can see more examples in this presentation: http://www.slideshare.net/SmalltalkWorld/stoop-302double-dispatch