Terminology – What is Dispatch and Does it Imply Dynamic Resolution?

terminology

AFAIK, the term dispatch means just a method resolution and calling. It doesn't matter whether it is static or dynamic. I saw many people are using a term like static dispatch and dynamic dispatch.

What makes me confusing is there're also some mysterious descriptions. I was trying to understand what is multiple dispatch, and it seems just selecting a subprogram by parameter types. If I understood it correctly, there can be both of static multiple dispatch and dynamic multiple dispatch, and we can say C++ is providing multiple dispatch via free functions.

But, Wikipedia article about multiple dispatch says C++ has no multiple dispatch because it doesn't have dynamic resolution of function by multiple parameters. And I really don't get conceptual difference between Common Lisp example and C++ overloaded function. Because I can't find any conceptual difference unless the term multiple dispatch implies dynamic dispatch. And I realized that I am confusing what the dispatching really is

I also checked QA entry Multiple Dispatch vs. Function Overloading, and it seems the answer is premising the term dispatch is basically dynamic. That also makes me confusing.

What is correct meaning of the term dispatch? Does it imply dynamic resolution? Is this term well defined or just conventional? What am I missing?

Best Answer

The terms mean the following:

  • static dispatch = the dispatch order is defined at compile time. It simply means that any function/method call say foo() or x.foo() will always invoke the very same function -- this is established once and then stays that way. This implies that the compiler can determine the type of x at compile time.

  • dynamic dispatch = the dispatch order is resolved at run time. This means the compiler builds a lookup table of all functions/methods and determines which one to actually call at run time. Say there there is class A and B, both of which implement interface X with method X.bar(). At runtime, y is examined and based on its actual class either A.bar() or B.bar() is called.

  • multiple dynamic dispatch = the dispatch order is dependent on function/method name + argument types (=a.k.a. signature), and the actual implementation that gets called is determined dynamically at runtime. Say class A implements methods A.fooBar(int) and A.fooBar(char *), and there is a call to a.fooBar(x) in your program. At runtime both a and x are examined and the actual method to call is determined based on the type of x.

See Wikipedia for more on dynamic dispatch and multiple dynamic dispatch.