Multiple dispatch

Developers of computer software typically organize source code into named blocks variously called subroutines, procedures, subprograms, functions, or methods.

It is sometimes desirable to give several functions the same name, often because they perform conceptually similar tasks, but operate on different types of input data.

In such cases, the name reference at the function call site is not sufficient for identifying the block of code to be executed.

In more conventional, i.e., single-dispatch object-oriented programming languages, when invoking a method (sending a message in Smalltalk, calling a member function in C++), one of its arguments is treated specially and used to determine which of the (potentially many) classes of methods of that name is to be applied.

In contrast, in languages with multiple dispatch, the selected method is simply the one whose arguments match the number and type of the function call.

Multiple dispatch should be distinguished from function overloading, in which static typing information, such as a term's declared or inferred type (or base type in a language with subtyping) is used to determine which of several possibilities will be used at a given call site, and that determination is made at compile or link time (or some other time before program execution starts) and is thereafter invariant for a given deployment or run of the program.

When working with languages that can discriminate data types at compile time, selecting among the alternatives can occur then.

In programming languages that defer data type identification until run time (i.e., late binding), selection among alternative functions must occur then, based on the dynamically determined types of function arguments.

In some languages,[citation needed] the distinction between overloading and multimethods can be blurred, with the compiler determining whether compile time selection can be applied to a given function call, or whether slower run time dispatch is needed.

With single dispatch and in the absence of multiple inheritance, this condition is trivially satisfied, but with multiple dispatch, it is possible for two or more candidates to satisfy a given actual argument list, but neither is more specific than the other (one dynamic argument being the subtype in one case, another being the subtype in the other case).

The "vtable" method developed in C++ and other early OO languages (where each class has an array of function pointers corresponding to that class's virtual functions) is nearly as fast as a static method call, requiring O(1) overhead and only one additional memory lookup even in the un-optimized case.

However, the vtable method uses the function name and not the argument type as its lookup key, and does not scale to the multiple dispatch case.

(It also depends on the object-oriented paradigm of methods being features of classes, not standalone entities independent of any particular datatype).

They analyzed nine applications, mostly compilers, written in six different languages: Common Lisp Object System, Dylan, Cecil, MultiJava, Diesel, and Nice.

The remaining 65–93% of generic functions have one concrete method (overrider), and thus are not considered to use the dynamic types of their arguments.

Multiple dispatch is used much more heavily in Julia, where multiple dispatch was a central design concept from the origin of the language: collecting the same statistics as Muschevici on the average number of methods per generic function, it was found that the Julia standard library uses more than double the amount of overloading than in the other languages analyzed by Muschevici, and more than 10 times in the case of binary operators.

[3] The Julia version of the example above might look like: Output: Raku, like Perl, uses proven ideas from other languages, and type systems have shown themselves to offer compelling advantages in compiler-side code analysis and powerful user-side semantics via multiple dispatch.

JavaScript and TypeScript do not support multimethods at the syntax level, but it is possible to add multiple dispatch via a library.

Here is a simple example in C: With the C Object System library,[19] C does support dynamic dispatch similar to CLOS.