On Mar 24, 2007, at 3:48 PM, Guyren Howe wrote: > 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
Actually you don't need to test types using IsA; you can get the compiler to do the work. I wrote an RBD column on double dispatch some time ago that walks through it. Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html>
