Talk:Multiple dispatch
From Wikipedia, the free encyclopedia
Is the Java example poorly chosen, since the following would work just as well (via method overloading), and doesn't need anything as exotic as multi-dispatch?
/* Example using run time type comparison via Java's "instanceof" operator */ class Asteroid extends Thing { public void collide_with(Asteroid other) { } public void collide_with(Spaceship other) { } } class Spaceship extends Thing { public void collide_with(Asteroid other) { } public void collide_with(Spaceship other) { } }
What am I missing?
Hi You are right. That would work, but thats not really Multiple dispatch. It would be multiple dispatch if you had
/*Note:The following code is illegal in Java, however it would be legal if Java supported Multiple-dispatch*/ class Thing{ public void collide_with(Thing other){ //Thing-Thing collision code } } class Test{ void Main(){ Thing t1 = new Asteroid(); Thing t2 = new Spaceship(); t1.collide_with(t2); //Multiple Dispatch would call Asteroid.collide_with(Spaceship) } }
In Single-dispatch systems, subtype polymorphism is *ONLY* permitted on the receiver object of the method call, so the above code would be illegal in Java. If Java supported Multiple Dispatch, this call would work because polymorphism would be applied to the parameters aswell, when selecting the version of collide_with method to call. Think of Multiple-dispatch as an advanced form of polymorphism. It is not supported by c++/java because there it has some overhead and possibility of message ambiguity during run-time.
155.140.121.227 16:40, 22 January 2007 (UTC)Asim Sinha
[edit] VB9 multimethods
Erik Meijer (one of VB9 designers) claims that Visual Basic 9 has support for multimethods, here and here. I don't know VB9 syntax, but it would be cool if someone should provide an example and clarify it. —The preceding unsigned comment was added by 161.53.243.217 (talk) 08:41, 22 February 2007 (UTC).