I think I understand multiple dispatch, but the example there is seemingly only single dispatch. People already think multiple dispatch is just the same as overloading, so which example might show it off better, and is actually multiple, as in 2+, not just one dispatch?
What kind of code might we want to show off there? I really like the syntax for e.g. polynomials.
f(x) = 2x^2 + 1
but even such simple code might put of users as too technical. I would like an example anyone can understand, general purpose, not technical computing or too mathematical. Though maybe with unitful, using the same juxtaposition trick.
[The example shown returns a set tuple, peopke not knowing might misunderstand multiple dispatch, itâs great you have tuples (or sets), and can return multiple valuesâŚ]
Are you referring to the âgreetâ example on the front page of julialang.org? That one returns a tuple, not a set. (Having the two invocations of greet on separate lines might nevertheless be easier to understand.)
Note quite. You are correct that overloading requires static knowledge of types. However multiple dispatch only describes the way method resolution works and does not specify whether this happens âat runtimeâ or âat compile timeâ.
In Julia specifically, usually only the semantics are specified and the what gets compiled away or not is mostly an implementation detail. Fortunately the compiler is rather smart and manages to do most dispatch at compile time if one writes reasonable code
Sometimes people refer to multiple dispatch that can be inferred statically, i.e. at compile time, as âstatic dispatchâ. Conversely, if dispatch happens at runtime it is sometimes called âdynamic dispatchâ. These terms are related to but somewhat different from âmultiple dispatchâ.
Unfortunately, people get the wrong impression of multiple dispatch (or rather as implemented in Julia) from the Wikipedia entry and everything that parrots it:
Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments.[1] This is a generalization of single-dispatch polymorphism where a function or method call is dynamically dispatched based on the derived type of the object on which the method has been called.
On the other hand, if you follow that primary source link, the linked introduction provides a fitting definition that doesnât assert runtime types need runtime dispatch. I couldnât figure out how to copy text from Google Books so hereâs a screenshot:
Considering that devirtualization statically dispatches calls over runtime types for single-dispatch languages, itâs not hard to imagine this happening for multiple dispatch. But itâs not natural to think of that when just looking up definitions, and when people have, they often think it doesnât count as multiple dispatch anymore. Even a little misinformation can cause a lot of headaches.
Multiple dispatch is the selection of a function implementation based on the types of each argument of the function.
We have not seen this in the literature but it seems worthwhile to point out four possibilities:
Static single dispatch (not done)
Static multiple dispatch (frequent in static languages, e.g. C++ overloading)
Dynamic single dispatch (Matlabâs object oriented system might fall in this category though
it has its own special characteristics)
Dynamic multiple dispatch (usually just called multiple dispatch).
Class-based object oriented programming could reasonably be called dynamic single dispatch, and overloading could reasonably be called static multiple dispatch. Juliaâs dynamic multiple dispatch approach is more flexible and adaptable while still retaining powerful performance capabilities
âDynamic multiple dispatchâ in that paper means dispatch over multiple dynamic types, not that the dispatch itself must be dynamic, which was never true even for âdynamic single dispatch.â We donât use those terms in practice because itâs ambiguous whatâs actually dynamic.