CSE 505 Lecture Notes:

Multiple dispatching

November 9, 1994


Single dispatch versus multiple dispatch languages

Most object-oriented languages (including Simula, Smalltalk, and C++) use single dispatching. The receiver of the message gets to decide what code to invoke. This works fine for messages with no arguments, or messages with "auxiliary" arguments:

x copy x printOn: stream It doesn't work so well for arithmetic operators, or messages with "interesting" arguments.

x + y x displayOn: device What to do?


Double Dispatching

Described by Dan Ingalls in an OOPSLA 86 paper.

Rectangle displayOn: device device displayRectangle: self Oval displayOn: device device displayOval: self Xterminal displayRectangle: r displayOval: o PostscriptFile displayRectangle: r displayOval: o ..... Float + y "the system will first try the primitive. If it fails (because y isn't a float), it will execute the Smalltalk code" ^ y floatPlus: self floatPlus: a ^ self + a integerPlus: a ^ a asFloat + self Integer + y ^ y integerPlus: self floatPlus: a ^ self asFloat + a integerPlus: a self + a

Multi-Methods

Languages using Multi-Methods:

Is it object-oriented?

CLOS: generic functions (function call, case-analysis style implementation)

Cecil: method is inside each of the arguments it discriminates on

Sample Cecil declarations:

x@smallint + y@smallint x@smallint + y@bigint x@bigint + y@smallint x@bigint + y@bigint asBigint(X@smallint) Each of these methods that has a smallint as an argument is "in" the smallint class.

Possible Problems


Kaleidoscope

Kaleidoscope is a constraint imperative language, which combines constraints with standard object-oriented programming

In Kaleidoscope, multiple dispatch is essential. Consider a + constraint. As in other object oriented languages, the meaning of + is determined by the classes of the arguments (+ means something different for floats and points).

x+y = z Suppose y and z are known, and x is unknown. In this case, we should definitely not dispatch on the class of x to decide what + means!