Double dispatch

Stefan Schlechtweg stefans at isgnw.CS.Uni-Magdeburg.De
Wed Nov 4 15:31:11 UTC 1998


Hi,

[document color stuff deleted]

I think you didn't describe double dispatching. What I understand as
double dispatching is the following:

If you want to know what's double dispatching, you might check the
VisualWorks Number classes (I'm new to Squeak and therefore just know
that Squeak does the same stuff somewhat different).

For instance Integer>>+ is implemented as follows:

	+ aNumber
		^aNumber sumFromInteger: self

This message send delegates the task to the actual argument (aNumber)
and passes on the class of the (actual) receiver in the message name
(sumFromInteger:). The receiver of this message then implements the
right behaviour for - in this case - adding an Integer value to itself.
Let's have some examples. In VisualWorks the method sumFromInteger: is
implemented in the class Float as follows:

	sumFromInteger: anInteger
		^anInteger asFloat + self

So it converts the integer argument into a float and then adds the
receiver. Another example, the class Fraction in VisualWorks implemets 

	sumFromInteger: anInteger
	"Reduction should not be necessary"
	^self species
		numerator: anInteger * denominator + numerator
		denominator: denominator

which actually creates a Fraction.

So double dispatching basically sends a message to the argument of the
message and passes on information about the class of the first receiver.
I hope I could make this clear somehow.

There might be more examples also in Squeak but the one I mentioned
above was the one which came in my mind immediately. The next time I
promise to chose an example from Squeak.

	Stefan

-- 
 --[ Stefan Schlechtweg ]-------[ stefans at isg.cs.uni-magdeburg.de ]--
| Otto-von-Guericke-Universitaet Magdeburg   TEL:  +49 391 67 128 61 |
| FIN, ISG, Uniplatz 2,  D-39106 Magdeburg   FAX:  +49 391 67 111 64 |
 ----------[ http://isgwww.cs.Uni-Magdeburg.DE/~stefans/ ]-----------





More information about the Squeak-dev mailing list