Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is - Single and Multiple Dispatch (in relation to .NET)?

Is it the same as overloading, if not, can you please provide and example of each in C#

I have read the responses to a similar question asked in SO ... i did not understand the responses posted to it.

Similar question asked here

EDIT: With the new "dynamic" keyword in C# 4.0 ... would this make the language "multi dispatch" enabled?

like image 568
Developer Avatar asked Jan 26 '09 16:01

Developer


3 Answers

Multiple dispatch is a "form" of overloading...

For example, C# is single dispatch because if works out what method to call based on only one argument, the "this" pointer. When you have something like this:

Base base= new Derived();
base.DoSomething();

the method Derived.DoSomething is called even though you called it through the base pointer. Now, if we've got the following:

class Derived : Base
{
  public override void Process(Stream stream);
  public override void Process(FileStream stream);
  public override void Process(MemoryStream stream);
}

And we do this:

Stream stream= new MemoryStream(...);
Base b= new Derived();
b.Process(stream);

Then we will call the Process(Stream) method from Derived as C# does a single dispatch on the object pointer (b) and then uses the compile time information to decide which method to call. Even though stream is a MemoryStream a single dispatch system will ignore this.

In a multi-dispatch system the object pointer will be looked at (as in C#) AND the runtime types of the arguments will be examined. In the above example, because stream is actually a MemoryStream the system would call the Process(MemoryStream) method.

like image 108
Sean Avatar answered Oct 18 '22 05:10

Sean


C# uses single dispatch, which includes overloaded methods. When you have the code

stringBuilder.Append(parameter);

the dispatcher looks at all methods defined on the stringBuilder's class, and finds the correct one.

For a multiple dispatch example, let's look at Prolog (which is the first one I could think of). You can define a function in prolog as such:

func(Arg1, Arg2) :- ....body....

This is not defined inside any class but in a global scope. Then, you can call func(Arg1, Arg2) on any two arguments and this function will be called. If you want something like overloading, you have to validate the argument types inside the function, and define it multiple times:

func(Arg1, Arg2) :- is_number(Arg1), is_string(Arg2), ....body....
func(Arg1, Arg2) :- is_string(Arg1), is_list(Arg2), ....body....
func(Arg1, Arg2) :- is_number(Arg1), is_list(Arg2), ....body....

Then, any two argument types you would send would both be checked - that is the multiple dispatch part.

In short, single dispatch only looks at methods defined on the first argument (in our first example, the stringBuilder), then resolves the correct overload to call using the other arguments. Multiple dispatch has methods/functions defined in the global scope and treats all arguments the same during overload resolution.

I hope I made myself clear, this is a pretty tough subject.


Update: I forgot to mention, multiple dispatch happens at runtime while single dispatch happens at compile time.
Update #2: Apparently, that was not true.

like image 38
configurator Avatar answered Oct 18 '22 04:10

configurator


Multi-dispatch is related to method overloading but not the same. The former is a dynamic runtime decision, the latter is a static compiletime decision. There is implicitly a flexibility benefit, but a performance cost to multi-dispatch therefore.

AFAIK a language can support either, but not both, though both can be simulated (multi-dispatch can be simulated with visitors). C# determines datatypes at compile time and therefore is not a multi-dispatch language, so no examples are possible.

(caveat: I'm not 100% on this)


addendum: actually wikipedia has an article on this which seems pretty thorough, ooh and a helpful LISP example

like image 29
annakata Avatar answered Oct 18 '22 06:10

annakata