Double dispatch in Pharo
Asked Answered
M

1

7

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.

Malpractice answered 15/6, 2015 at 17:56 Comment(0)
M
13

Essentially the idea is that you have method:

  • #addInteger: which knows how to add integers,
  • #addFloat: which knows how to add floats,
  • and so on…

Now in Integer class you define + as:

+ otherObject

   otherObject addInteger: self

in Float you define it like:

+ otherObject

   otherObject addFloat: self

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 + in Point class is defined as:

+ arg 

   arg isPoint ifTrue: [^ (x + arg x) @ (y + arg y)].
   ^ arg adaptToPoint: self andSend: #+

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:

adaptToPoint: rcvr andSend: selector

   ^ self collect: [:element | rcvr perform: selector with: element]

and Number implements it like that:

adaptToPoint: rcvr andSend: selector

   ^ rcvr perform: selector with: self@self

Note, that to avoid explicit type checking, we could define that method in the Point itself this way:

adaptToPoint: rcvr andSend: selector

   ^ (x perform: selector with: arg x) @ (y perform: selector with: arg y)

You can see more examples in this presentation: http://www.slideshare.net/SmalltalkWorld/stoop-302double-dispatch

Medalist answered 15/6, 2015 at 22:55 Comment(1)
Thank you, that makes perfect sense now. Basically you call a generic method, define it in all related classes. But within the specific classes that you want the arguments to actually go to, you put some specific implementation with the specific arguments. When the method call is determined, it figure out which method to call based on selector and reciever of the object.Malpractice

© 2022 - 2024 — McMap. All rights reserved.