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
Contents |
[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).
[edit] Critique?
This article contains a sore lack of critique of multiple dispatch as a technique. What are the pros and cons of it? RyanTMulligan 23:46, 9 June 2007 (UTC)
[edit] Re: Critique?
Multiple dispatch is often essential in software engineering like Visitor pattern. When you have a very dynamic and potentially extensible system where user defined types need to interact with each other in a polymorphic way, multiple dispatch provides the most flexible and intuitive solutions. Even Bjarne Stroustrup admitted that he rejected multi-methods with regret because he couldn't find an acceptable way to implement it in c++. Cons of multiple dispatch are that its harder to prove type safety, incurs atleast some overhead at run-time (to determine the best method to dispatch) and possibility of message ambiguity at run-time. Considering that Java and .Net are providing more run-time type-checking capabilities with each new version, the small overhead in multiple dispatch should not be a limiting factor anymore. I truly think languages could benefit hugely by implementing multiple dispatch.
[edit] Static types vs. dynamic types
Would it be useful to note the difference between static types and dynamic types (and how multiple dispatch operates on the dynamic, aka run-time type) before the C++ section? Text such as
When working with languages that can discriminate data types at compile-time, selecting among the alternatives can occur at compile-time.
makes it unclear as to whether we are talking about static types or dynamic types. The above would seem to indicate that any statically typed language that allows function overloading would allow multiple dispatch, but this is not the case. In fact, is the above actually true? How could one always determine run-time data types at compile time?
Labreuer 15:41, 29 October 2007 (UTC)
[edit] Java without multiple dispatch?
Is the following code segment not multiple dispatch? I realize that Java is not selecting the method at runtime, but instead at compile time (which separates it from dynamic dispatch). Perhaps added clarification in the article would help:
public class Manipulator {
public void manipulate(List list) {
System.out.println("List");
}
public void manipulate(Set set) {
System.out.println("Set");
}
public void manipulate(Queue queue) {
System.out.println("Queue");
}
}
class UseMan {
public static void main(String[] args) {
new Manipulator().manipulate(new HashSet());
new Manipulator().manipulate(new ArrayList());
// new Manipulator().manipulate(new LinkedList());
// line above is a compiler error - ambiguous invocation
}
}
The Java code above prints:
'Set' 'List'
Looks and smells like multiple dispatch to me - certainly not dynamic dispatch. Is this not right? .digamma (talk) 22:04, 16 February 2008 (UTC)
- To me this is not multiple dispatch, however I may be wrong because I just looked at this technique :)
- What you are doing here is method overloading (same name, different arguments) and this can be achieved statically (at compilation). —Preceding unsigned comment added by 62.17.152.241 (talk) 17:21, 21 February 2008 (UTC)
- Yes, this is method overloading but method overloading is a key aspect of multiple dispatch (see article). .digamma (talk) 11:14, 2 March 2008 (UTC)
- yes it is method overloading, but what makes you think it is multiple dispatch? The choice what method to execute depends only on the (dynamic) type of the argument of maniplate(), that is: only on the type of ONE object, whereas in multiple dispatch it depends on the type of MORE THAN ONE objects. It doesn't matter how many Interfaces the type of this ONE object implements, it's still just one object. 84.74.154.33 (talk) 20:27, 22 May 2008 (UTC)
- Yes, this is method overloading but method overloading is a key aspect of multiple dispatch (see article). .digamma (talk) 11:14, 2 March 2008 (UTC)

