On Mar 24, 2007, at 2:30 PM, gary hayenga wrote:

> What *is* multiple dispatch, and how is it useful, by the way?

I agree that multiple dispatch would be a marvelous feature, but I'm  
sure it's rather a lot more work than multiple return values would be.

Anyway.

Multiple dispatch means that rather than just being able to use  
polymorphic choice between different methods based on the object  
you're applying a method call to, you choose based on all the  
arguments to a method call.

That sentence is almost entirely opaque to most people. So here's  
another way of looking at it: Multiple Dispatch is overloading on  
steroids. It means REALbasic would do its darndest to find the best  
match to a method call, taking into account the idea that it can  
choose one method over another if it gets to choose a more specific  
type.

Example: you have classes A, SubA and SubSubA, each a subclass of the  
one before it.

If I have the following method signatures:

MySub(x As A, y As A) //1

MySub(x As SubA, y As A) //2

MySub(x As A, y As SubA) //3

MySub(x As SubA, y As Suba) //4

MySub(x As SubSubA, y As A) //5


Then each of these calls will call the indicated method:

MySub New A, New A //1

MySub New SubA, New A //2

MySub New SubA, New SubA //Error: ambiguous whether to use 2 or 3

MySub New SubSubA, New SubA //5


How is this useful? Almost every place in your program where you've  
used IsA can be eliminated in favor of this sort of smart overloading.

As an example, you might be writing a game where you have all sorts  
of sprites zooming around and colliding, and you need to do all sorts  
of different things, depending on what collided with what. As things  
stand, you can use polymorphic method dispatch on one of the sprite  
types, but not on the other one. So you'll have something like this:

//On Ship sprite class:

Sub CollisionWith (s As Sprite)

If s IsA PowerBullet Then
   //die immediately
ElseIf s IsA WimpyBullet Then
   //lose small hit points
ElseIf s IsA Bullet Then
   //lose regular hit points
ElseIf s IsA BigRock Then

...etc

Whereas with multiple dispatch, you can just write a set of different  
CollisionWith methods, each having different types.

SubCollisionWith (b As Powerbullet)
Sub CollisionWith (s As Bullet)

...etc

If you're doing a complex program, Multiple Dispatch can simplify  
your code a great deal, in some cases, by removing all these case  
statements and code duplication and the requirement to maintain  
those. In the former case above, if you add a new bullet type, you  
have to go through that case statement for every class it can collide  
with, and add the logic for hitting the new bullet type. But under  
multiple dispatch, you just add a new method for the collision cases  
where the new bullet type behaves differently.

Is that crystal? :-)


Regards,

Guyren G Howe
Relevant Logic LLC

guyren-at-relevantlogic.com ~ http://relevantlogic.com

REALbasic, PHP, Ruby/Rails, Python programming
PostgreSQL, MySQL database design and consulting
Technical writing and training


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to