[comp.lang.smalltalk] Message dispatch on argument class

sa1z+@andrew.cmu.edu (Sudheer Apte) (11/07/88)

Suppose I want the same selector to behave differently depending upon the class
of the argument. In Smalltalk (I run ParcPlace V2.2 on a Sun 3/50) I have to
write the method thus:

add: anObject

(anObject class == Class1) ifTrue:
     [^self privateAdd1: anObject]
ifFalse:
     ["Code dealing with all other valid classes"]

I have to make this decision within the method because I have no way
in Smalltalk to specify different methods to be invoked by the same message
selector depending upon the class of the argument. It seems this problem has
been well researched, and some kind of solution has been found which does not
sacrifice speed. Anyone know anything about this?

Thanks,
Sudheer.
_____________

budd@mist.cs.orst.edu (Tim Budd) (11/08/88)

There is a technique called double polymorphism that does exactly what
you want.  It is described in more detail in a paper by Dave Unger in the
first OOPSLA conference.  Basically, you turn the argument around, and
encode the type of the receiver for the first message in the message
selector for a second message.  Let me use mixed type arithmetic for an 
example.  Class Integer would have something like the following:
	+ x
		^ x addToInteger: self
	addToFloat: x
		^ x + self asFloat

A class like Float would, when it receives the message addToInteger, 
know that it must convert the argument into float before adding.
Here are a couple methods in class Float.
	+ x
		^ x addToFloat: self
	addToInteger: x
		^ x asFloat + x
The primitive methods are now addToInteger in class Integer and addToFloat
in class Float.

kentb@Apple.COM (Kent Beck) (11/08/88)

In response to Tim Budd's comments about double polymorphism- that's
Ungar not Unger, and anyway, Dan Ingalls wrote the article.  My little
contribution to the nit-pickiness of the world today.

Kent

johnson@p.cs.uiuc.edu (11/09/88)

Dan Ingalls wrote the paper on multiple polymorphism in OOPSLA'86,
not Dave Ungar.

Multiple polymorphism (or double dispatching, as I like to call it) works
just fine.  Kurt Hebel reimplemented all of arithmetic in Smalltalk-80
use double dispatching.  He also build a browser that makes it a lot
easier to use and releaves the burden of all the little methods.  We
have nearly finished a paper describing it.

soiffer@tekchips.CRL.TEK.COM (Neil Soiffer) (11/11/88)

As mentioned earlier, multiple polymorphism was discussed in Ingalls'
paper in OOPSLA '86.  What hasn't been mentioned is why this is better
than a "typecase".  The reason is modularity and reuse.  If a new type
is added and typecase is used, it is likely that old code must be modified
(ie, a new clause added to typecases).  If multiple dispatch is used,
then no old code needs to be modified, only new code added to handle the
new type.

In Smalltalk's number code, "isKindOf" and "isMemberOf" are used in a
modular way -- they only test the current type and if a match is not found,
then the message is "resent" to a more generic handler.

	Neil Soiffer

PS to Kent Beck:  I grew you a few nits to harvest in this message.