Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Multiple dispatch
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Feature of some programming languages}} {{Polymorphism}} '''Multiple dispatch''' or '''multimethods''' is a feature of some [[programming language]]s in which a [[Subroutine|function]] or [[Method (computer programming)|method]] can be [[dynamic dispatch|dynamically dispatched]] based on the [[run time (program lifecycle phase)|run-time]] (dynamic) type or, in the more general case, some other attribute of more than one of its [[Parameter (computer programming)|arguments]].<ref>{{cite book |url=https://books.google.com/books?id=Dd7nyto72sUC&q=%22multiple+dispatch+languages%2C+more+than+one+polymorphic+arguments%22&pg=PA260 |title=Contemporary Computing: Second International Conference, IC3 2010, Noida, India, August 9–11, 2010. Proceedings |last1=Ranka |first1=Sanjay |last2=Banerjee |first2=Arunava |last3=Biswas |first3=Kanad Kishore |last4=Dua |first4=Sumeet |last5=Mishra |first5=Prabhat |last6=Moona |first6=Rajat |publisher=Springer |date=2010-07-26 |isbn=9783642148248}}</ref> This is a generalization of [[single-dispatch]] [[polymorphism in object-oriented programming|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. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments. == Understanding dispatch == Developers of computer software typically organize [[source code]] into named blocks variously called [[subroutine]]s, procedures, subprograms, functions, or methods. The code in the function is executed by ''calling'' it – executing a piece of code that references its ''name''. This transfers control temporarily to the called function; when the function's execution has completed, control is typically transferred back to the instruction in the ''caller'' that follows the reference. Function names are usually selected so as to be descriptive of the function's purpose. 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. Instead, the number and type of the arguments to the function call are also used to select among several function implementations. In more conventional, i.e., [[Dynamic dispatch#Single and multiple dispatch|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 many languages, the ''special'' argument is indicated syntactically; for example, a number of programming languages put the special argument before a dot in making a method call: <code>special.method(other, arguments, here)</code>, so that <code>lion.sound()</code> would produce a roar, whereas <code>sparrow.sound()</code> would produce a chirp. 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. There is no ''special'' argument that ''owns'' the function/method carried out in a particular 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. Many languages such as C++ offer robust function overloading but do not offer dynamic multiple dispatch (C++ only permits dynamic single dispatch through use of virtual functions). === Data types === When working with languages that can discriminate [[data type]]s at [[compile time]], selecting among the alternatives can occur then. The act of creating such alternative functions for compile time selection is usually referred to as [[Function overloading|overloading]] a function. 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. Functions whose alternative implementations are selected in this manner are referred to most generally as ''multimethods''. There is some run-time cost associated with dynamically dispatching function calls. In some languages,{{Citation needed|date=December 2007}} 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. === Issues === There are several known issues with dynamic-dispatch, both single and multiple. While many of these issues are solved for single-dispatch, which has been a standard feature in object-oriented programming languages for decades, these issues become more complicated in the multiple-dispatch case. ====Expressiveness and modularity==== In most popular programming languages, source code is delivered and deployed in granules of functionality which we will here call ''packages''; actual terminology for this concept varies between language. Each package may contain multiple type, value, and function definitions, packages are often compiled separately in languages with a compilation step, and a non-cyclical dependency relationship may exist. A complete program is a set of packages, with a ''main package'' which may depend on several other packages, and the whole program consisting of the transitive closure of the dependency relationship. The so-called [[expression problem]] relates to the ability for code in a depending package to extend behaviors (functions or datatypes) defined in a base package from within an including package, without modifying the source to the base package. Traditional single-dispatch OO languages make it trivial to add new datatypes but not new functions; traditional functional languages tend to have the opposite effect, and multiple dispatch, if implemented correctly, allows both. It is desirable for an implementation of multiple dispatch to have the following properties: * It is possible to define different "cases" of a multi-method from within different packages without modifying the source of a base package. * Inclusion of another package in the program should not change the behavior of a given multi-method call, when the call does not use any datatypes defined in the package. * Conversely, if a datatype is defined in a given package, and a multi-method extension using that type is also defined in the same package, and a value of that type is passed (through a base type reference or into a generic function) into another package with no dependency on that package, and then the multi-method is invoked with that value as an argument, the multi-method case defined in the package which includes the type should be employed. To put it another way—within a given program, the same multi-method invoked with the same set of arguments should resolve to the same implementation, regardless of the location of the call site, and whether or not a given definition is "in scope" or "visible" at the point of the method call. ====Ambiguity==== It is generally desirable that for any given invocation of a multi-method, there be at most one "best" candidate among implementation cases of the multi-method, and/or that if there is not, that this be resolved in a predictable and deterministic fashion, including failure. Non-deterministic behavior is undesirable. Assuming a set of types with a non-circular subtyping relationship, one can define that one implementation of a multi-method is "better" (more specific) if all dynamically-dispatched arguments in the first are subtypes of all dynamically-dispatched arguments specified in the second, and at least one is a strict subtype. 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). This particularly can happen if two different packages, neither depending on the other, both extend some multi-method with implementations concerning each package's types, and then a third package that includes both (possibly indirectly) then invokes the multi-method using arguments from both packages. Possible resolutions include: * Treating any ambiguous calls as an error. This might be caught at compile time (or otherwise before deployment), but might not be detected until runtime and produce a runtime error. * Ordering the arguments, so e.g. the case with the most specific first argument is selected, and subsequent arguments are not considered for ambiguity resolution unless the first argument is insufficient to resolve the issue. * Construction of other rules for resolving an ambiguity in one direction or another. Sometimes, such rules might be arbitrary and surprising. In the rules for static overload resolution in C++, for instance, a type which matches exactly is understandably considered a better match than a type which matches through a base type reference or a generic (template) parameter. However, if the only possible matches are either through a base type or a generic parameter, the generic parameter is preferred over the base type, a rule that sometimes produces surprising behavior. ====Efficiency==== Efficient implementation of single-dispatch, including in programming languages that are separately compiled to object code and linked with a low-level (not-language-aware) linker, including dynamically at program load/start time or even under the direction of the application code, are well known. 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). Efficient implementation of multiple-dispatch remains an ongoing research problem. === Use in practice === To estimate how often multiple dispatch is used in practice, Muschevici et al.<ref name=Muschevici>{{cite book |last1=Muschevici |first1=Radu |last2=Potanin |first2=Alex |last3=Tempero |first3=Ewan |last4=Noble |first4=James |title=Proceedings of the 23rd ACM SIGPLAN conference on Object-oriented programming systems languages and applications |chapter=Multiple dispatch in practice |year=2008 |series=OOPSLA '08 |pages=563–582 |doi=10.1145/1449764.1449808 |publisher=ACM |location=Nashville, TN, USA |isbn=9781605582153|s2cid=7605233 |url=https://figshare.com/articles/thesis/Multiple_Dispatch_in_Practice/16959112 }}</ref> studied programs that use dynamic dispatch. They analyzed nine applications, mostly compilers, written in six different languages: [[Common Lisp Object System]], [[Dylan (programming language)|Dylan]], [[Cecil (programming language)|Cecil]], MultiJava, Diesel, and Nice. Their results show that 13–32% of generic functions use the dynamic type of one argument, while 2.7–6.5% of them use the dynamic type of multiple arguments. 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. Further, the study reports that 2–20% of generic functions had two and 3–6% had three concrete function implementations. The numbers decrease rapidly for functions with more concrete overriders. Multiple dispatch is used much more heavily in [[Julia (programming language)|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 [[Operator (computer programming)|binary operators]].<ref name=julia-review/> The data from these papers is summarized in the following table, where the dispatch ratio <code>DR</code> is the average number of methods per generic function; the choice ratio <code>CR</code> is the mean of the square of the number of methods (to better measure the frequency of functions with a large number of methods);<ref name=Muschevici/><ref name=julia-review/> and the degree of specialization <code>DoS</code> is the average number of type-specialized arguments per method (i.e., the number of arguments that are dispatched on): {| class="wikitable sortable" |- ! Language ! Average # methods (DR) ! Choice ratio (CR) ! Degree of specialization (DoS) |- | [[Cecil (programming language)|Cecil]]<ref name=Muschevici/> | 2.33 | 63.30 | 1.06 |- | [[Common Lisp]] ([[CMU Common Lisp|CMU]])<ref name=Muschevici/> | 2.03 | 6.34 | 1.17 |- | Common Lisp ([[McCLIM]])<ref name=Muschevici/> | 2.32 | 15.43 | 1.17 |- | Common Lisp ([[Steel Bank Common Lisp|Steel Bank]])<ref name=Muschevici/> | 2.37 | 26.57 | 1.11 |- | Diesel<ref name=Muschevici/> | 2.07 | 31.65 | 0.71 |- | [[Dylan (programming language)|Dylan]] (Gwydion)<ref name=Muschevici/> | 1.74 | 18.27 | 2.14 |- | Dylan (OpenDylan)<ref name=Muschevici/> | 2.51 | 43.84 | 1.23 |- | [[Julia (programming language)|Julia]]<ref name=julia-review/> | 5.86 | 51.44 | 1.54 |- | Julia (operators only)<ref name=julia-review/> | 28.13 | 78.06 | 2.01 |- | MultiJava<ref name=Muschevici/> | 1.50 | 8.92 | 1.02 |- | Nice<ref name=Muschevici/> | 1.36 | 3.46 | 0.33 |- |} === Theory === The theory of multiple dispatching languages was first developed by Castagna et al., by defining a model for overloaded functions with [[late binding]].<ref>{{cite journal |last1=Castagna |first1=Giuseppe |last2=Ghelli |first2=Giorgio |last3=Longo |first3=Giuseppe |name-list-style=amp |year=1995 |title=A calculus for overloaded functions with subtyping |journal=Information and Computation |volume=117 |issue=1 |pages=115–135 |doi=10.1006/inco.1995.1033 |doi-access=free }}</ref><ref>{{cite book |last=Castagna |first=Giuseppe |title=Object-Oriented Programming: A Unified Foundation |url=https://www.springer.com/birkhauser/computer+science/book/978-0-8176-3905-1 |year=1996 |publisher=Birkhäuser |isbn=978-0-8176-3905-1 |page=384 |series=Progress in Theoretical Computer Science}}</ref> It yielded the first formalization of the [[Covariance and contravariance (computer science)|problem of covariance and contravariance]] of object-oriented languages<ref>{{cite journal |last=Castagna |first=Giuseppe |year=1995 |title=Covariance and contravariance: conflict without a cause |journal=ACM Transactions on Programming Languages and Systems|volume=17 |issue=3 |pages=431–447 |doi=10.1145/203095.203096 |citeseerx=10.1.1.115.5992|s2cid=15402223 }}</ref> and a solution to the problem of binary methods.<ref>{{cite journal |last1=Bruce |first1=Kim |last2=Cardelli |first2=Luca |last3=Castagna |first3=Giuseppe |last4=Leavens |first4=Gary T. |last5=Pierce |first5=Benjamin |year=1995 |title=On binary methods |journal=Theory and Practice of Object Systems |volume=1 |issue=3 |pages= 221–242|doi= 10.1002/j.1096-9942.1995.tb00019.x|url=http://dl.acm.org/citation.cfm?id=230849.230854 |access-date=2013-04-19|url-access=subscription }}</ref> == Examples == Distinguishing multiple and single dispatch may be made clearer by an example. Imagine a game that has, among its (user-visible) objects, spaceships and asteroids. When two objects collide, the program may need to do different things according to what has just hit what. === Languages with built-in multiple dispatch === ==== C# ==== [[C Sharp (programming language)|C#]] introduced support for dynamic multimethods in version 4<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic |title=Using type dynamic (C# Programming Guide) |access-date=2020-05-14 }}</ref> (April 2010) using the 'dynamic' keyword. The following example demonstrates multimethods. Like many other statically-typed languages, C# also supports static method overloading.<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#signatures-and-overloading |title=Basic concepts |access-date=2020-05-14 }}</ref> Microsoft expects that developers will choose static typing over dynamic typing in most scenarios.<ref>{{cite web |url=https://docs.microsoft.com/en-us/archive/msdn-magazine/2011/february/msdn-magazine-dynamic-net-understanding-the-dynamic-keyword-in-csharp-4 |title=Dynamic .NET - Understanding the Dynamic Keyword in C# 4 |date=10 August 2015 |access-date=2020-05-14 }}</ref> The 'dynamic' keyword supports interoperability with COM objects and dynamically-typed .NET languages. [[File:Csharp ColliderLibrary.svg]] The example below uses features introduced in C# 9 and C# 10. <syntaxhighlight lang="c#"> using static ColliderLibrary; Console.WriteLine(Collide(new Asteroid(101), new Spaceship(300))); Console.WriteLine(Collide(new Asteroid(10), new Spaceship(10))); Console.WriteLine(Collide(new Spaceship(101), new Spaceship(10))); string Collide(SpaceObject x, SpaceObject y) => x.Size > 100 && y.Size > 100 ? "Big boom!" : CollideWith(x as dynamic, y as dynamic); // Dynamic dispatch to CollideWith method class ColliderLibrary { public static string CollideWith(Asteroid x, Asteroid y) => "a/a"; public static string CollideWith(Asteroid x, Spaceship y) => "a/s"; public static string CollideWith(Spaceship x, Asteroid y) => "s/a"; public static string CollideWith(Spaceship x, Spaceship y) => "s/s"; } abstract record SpaceObject(int Size); record Asteroid(int Size) : SpaceObject(Size); record Spaceship(int Size) : SpaceObject(Size); </syntaxhighlight> Output: <syntaxhighlight lang="output"> Big boom! a/s s/s </syntaxhighlight> ==== Groovy ==== [[Apache Groovy|Groovy]] is a general purpose [[Java (programming language)|Java]] compatible/interusable [[Java virtual machine|JVM]] language, which, contrary to Java, uses late binding / multiple dispatch.<ref>[https://groovy-lang.org/differences.html#_multi_methods Groovy - Multi-methods]</ref> <syntaxhighlight lang="groovy"> /* Groovy implementation of C# example above Late binding works the same when using non-static methods or compiling class/methods statically (@CompileStatic annotation) */ class Program { static void main(String[] args) { println Collider.collide(new Asteroid(101), new Spaceship(300)) println Collider.collide(new Asteroid(10), new Spaceship(10)) println Collider.collide(new Spaceship(101), new Spaceship(10)) } } class Collider { static String collide(SpaceObject x, SpaceObject y) { (x.size > 100 && y.size > 100) ? "big-boom" : collideWith(x, y) // Dynamic dispatch to collideWith method } private static String collideWith(Asteroid x, Asteroid y) { "a/a" } private static String collideWith(Asteroid x, Spaceship y) { "a/s" } private static String collideWith(Spaceship x, Asteroid y) { "s/a" } private static String collideWith(Spaceship x, Spaceship y) { "s/s"} } class SpaceObject { int size SpaceObject(int size) { this.size = size } } @InheritConstructors class Asteroid extends SpaceObject {} @InheritConstructors class Spaceship extends SpaceObject {} </syntaxhighlight> ==== Common Lisp ==== In a language with multiple dispatch, such as [[Common Lisp]], it might look more like this (Common Lisp example shown): <syntaxhighlight lang="lisp"> (defclass asteroid () ((size :reader size :initarg :size))) (defclass spaceship () ((size :reader size :initarg :size))) (defun space-object (class size) (make-instance class :size size)) ; collide-with is a generic function with multiple dispatch (defmethod collide-with ((x asteroid) (y asteroid)) "a/a") (defmethod collide-with ((x asteroid) (y spaceship)) "a/s") (defmethod collide-with ((x spaceship) (y asteroid)) "s/a") (defmethod collide-with ((x spaceship) (y spaceship)) "s/s") (defun collide (x y) (if (and (> (size x) 100) (> (size y) 100)) "big-boom" (collide-with x y))) (print (collide (space-object 'asteroid 101) (space-object 'spaceship 300))) (print (collide (space-object 'asteroid 10) (space-object 'spaceship 10))) (print (collide (space-object 'spaceship 101) (space-object 'spaceship 10))) </syntaxhighlight> and similarly for the other methods. Explicit testing and "dynamic casting" are not used. In the presence of multiple dispatch, the traditional idea of methods as being defined in classes and contained in objects becomes less appealing—each ''collide-with'' method above is attached to two different classes, not one. Hence, the special syntax for method invocation generally disappears, so that method invocation looks exactly like ordinary function invocation, and methods are grouped not in classes but in [[generic function]]s. ==== Julia ==== [[Julia (programming language)|Julia]] has built-in multiple dispatch, and it is central to the language design.<ref name=julia-review>{{cite journal |last1=Bezanson |first1=Jeff |last2=Edelman |first2=Alan |last3=Karpinski |first3=Stefan |last4=Shah |first4=Viral B. |title=Julia: A fresh approach to numerical computing |journal=SIAM Review |volume=59 |issue=1 |pages=65–98 |date=7 February 2017 |doi=10.1137/141000671 |arxiv=1411.1607|s2cid=13026838 }}</ref> The Julia version of the example above might look like: <syntaxhighlight lang="julia"> abstract type SpaceObject end struct Asteroid <: SpaceObject size::Int end struct Spaceship <: SpaceObject size::Int end collide_with(::Asteroid, ::Spaceship) = "a/s" collide_with(::Spaceship, ::Asteroid) = "s/a" collide_with(::Spaceship, ::Spaceship) = "s/s" collide_with(::Asteroid, ::Asteroid) = "a/a" collide(x::SpaceObject, y::SpaceObject) = (x.size > 100 && y.size > 100) ? "Big boom!" : collide_with(x, y) </syntaxhighlight> Output: <syntaxhighlight lang="julia-repl"> julia> collide(Asteroid(101), Spaceship(300)) "Big boom!" julia> collide(Asteroid(10), Spaceship(10)) "a/s" julia> collide(Spaceship(101), Spaceship(10)) "s/s" </syntaxhighlight> ==== Raku ==== [[Raku (programming language)|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. It has both multimethods, and multisubs. Since most operators are subroutines, it also has multiple dispatched operators. Along with the usual type constraints, it also has ''where'' constraints that allow making very specialized subroutines. <syntaxhighlight lang="raku"> subset Mass of Real where 0 ^..^ Inf; role Stellar-Object { has Mass $.mass is required; method name () returns Str {...}; } class Asteroid does Stellar-Object { method name () { 'an asteroid' } } class Spaceship does Stellar-Object { has Str $.name = 'some unnamed spaceship'; } my Str @destroyed = < obliterated destroyed mangled >; my Str @damaged = « damaged 'collided with' 'was damaged by' »; # We add multi candidates to the numeric comparison operators because we are comparing them numerically, # but makes no sense to have the objects coerce to a Numeric type. # ( If they did coerce we wouldn't necessarily need to add these operators. ) # We could have also defined entirely new operators this same way. multi sub infix:« <=> » ( Stellar-Object:D $a, Stellar-Object:D $b ) { $a.mass <=> $b.mass } multi sub infix:« < » ( Stellar-Object:D $a, Stellar-Object:D $b ) { $a.mass < $b.mass } multi sub infix:« > » ( Stellar-Object:D $a, Stellar-Object:D $b ) { $a.mass > $b.mass } multi sub infix:« == » ( Stellar-Object:D $a, Stellar-Object:D $b ) { $a.mass == $b.mass } # Define a new multi dispatcher, and add some type constraints to the parameters. # If we didn't define it we would have gotten a generic one that didn't have constraints. proto sub collide ( Stellar-Object:D $, Stellar-Object:D $ ) {*} # No need to repeat the types here since they are the same as the prototype. # The 'where' constraint technically only applies to $b not the whole signature. # Note that the 'where' constraint uses the `<` operator candidate we added earlier. multi sub collide ( $a, $b where $a < $b ) { say "$a.name() was @destroyed.pick() by $b.name()"; } multi sub collide ( $a, $b where $a > $b ) { # redispatch to the previous candidate with the arguments swapped samewith $b, $a; } # This has to be after the first two because the other ones # have 'where' constraints, which get checked in the # order the subs were written. ( This one would always match. ) multi sub collide ( $a, $b ) { # randomize the order my ($n1, $n2) = ( $a.name, $b.name ).pick(*); say "$n1 @damaged.pick() $n2"; } # The following two candidates can be anywhere after the proto, # because they have more specialized types than the preceding three. # If the ships have unequal mass one of the first two candidates gets called instead. multi sub collide ( Spaceship $a, Spaceship $b where $a == $b ){ my ($n1, $n2) = ( $a.name, $b.name ).pick(*); say "$n1 collided with $n2, and both ships were ", ( @destroyed.pick, 'left damaged' ).pick; } # You can unpack the attributes into variables within the signature. # You could even have a constraint on them `(:mass($a) where 10)`. multi sub collide ( Asteroid $ (:mass($a)), Asteroid $ (:mass($b)) ){ say "two asteroids collided and combined into one larger asteroid of mass { $a + $b }"; } my Spaceship $Enterprise .= new(:mass(1),:name('The Enterprise')); collide Asteroid.new(:mass(.1)), $Enterprise; collide $Enterprise, Spaceship.new(:mass(.1)); collide $Enterprise, Asteroid.new(:mass(1)); collide $Enterprise, Spaceship.new(:mass(1)); collide Asteroid.new(:mass(10)), Asteroid.new(:mass(5)); </syntaxhighlight> === Extending languages with multiple-dispatch libraries === ==== JavaScript ==== In languages that do not support multiple dispatch at the language definition or syntactic level, it is often possible to add multiple dispatch using a [[Library (computing)|library]] extension. JavaScript and TypeScript do not support multimethods at the syntax level, but it is possible to add multiple dispatch via a library. For example, the ''multimethod package''<ref name="multimethod_package">[https://www.npmjs.com/package/@arrows/multimethod @arrows/multimethod] Multiple dispatch in JavaScript/TypeScript with configurable dispatch resolution by Maciej Cąderek.</ref> provides an implementation of multiple dispatch, generic functions. Dynamically-typed version in JavaScript: <syntaxhighlight lang="javascript"> import { multi, method } from '@arrows/multimethod' class Asteroid {} class Spaceship {} const collideWith = multi( method([Asteroid, Asteroid], (x, y) => { // deal with asteroid hitting asteroid }), method([Asteroid, Spaceship], (x, y) => { // deal with asteroid hitting spaceship }), method([Spaceship, Asteroid], (x, y) => { // deal with spaceship hitting asteroid }), method([Spaceship, Spaceship], (x, y) => { // deal with spaceship hitting spaceship }), ) </syntaxhighlight> Statically-typed version in TypeScript: <syntaxhighlight lang="typescript"> import { multi, method, Multi } from '@arrows/multimethod' class Asteroid {} class Spaceship {} type CollideWith = Multi & { (x: Asteroid, y: Asteroid): void (x: Asteroid, y: Spaceship): void (x: Spaceship, y: Asteroid): void (x: Spaceship, y: Spaceship): void } const collideWith: CollideWith = multi( method([Asteroid, Asteroid], (x, y) => { // deal with asteroid hitting asteroid }), method([Asteroid, Spaceship], (x, y) => { // deal with asteroid hitting spaceship }), method([Spaceship, Asteroid], (x, y) => { // deal with spaceship hitting asteroid }), method([Spaceship, Spaceship], (x, y) => { // deal with spaceship hitting spaceship }), ) </syntaxhighlight> ==== Python ==== Multiple dispatch can be added to [[Python (programming language)|Python]] using a [[Library (computing)|library]] extension. For example, using the module ''multimethod.py''<ref>{{Citation|last=Coady|first=Aric|title=multimethod: Multiple argument dispatching.|url=https://github.com/coady/multimethod|access-date=2021-01-28}}</ref> and also with the module ''multimethods.py''<ref name="multimethods_module">[http://gnosis.cx/download/gnosis/magic/multimethods.py multimethods.py] {{Webarchive|url=https://web.archive.org/web/20050309230813/http://gnosis.cx/download/gnosis/magic/multimethods.py |date=2005-03-09 }}, Multiple dispatch in Python with configurable dispatch resolution by David Mertz, et al.</ref> which provides CLOS-style multimethods for [[Python (programming language)|Python]] without changing the underlying syntax or keywords of the language. <syntaxhighlight lang="python"> from multimethods import Dispatch from game_objects import Asteroid, Spaceship from game_behaviors import as_func, ss_func, sa_func collide = Dispatch() collide.add_rule((Asteroid, Spaceship), as_func) collide.add_rule((Spaceship, Spaceship), ss_func) collide.add_rule((Spaceship, Asteroid), sa_func) def aa_func(a, b): """Behavior when asteroid hits asteroid.""" # ...define new behavior... collide.add_rule((Asteroid, Asteroid), aa_func) </syntaxhighlight> <syntaxhighlight lang="python"> # ...later... collide(thing1, thing2) </syntaxhighlight> Functionally, this is very similar to the CLOS example, but the syntax is conventional Python. Using Python 2.4 [[Python syntax and semantics#Decorators|decorators]], [[Guido van Rossum]] produced a sample implementation of multimethods<ref>{{Cite web | url=http://www.artima.com/weblogs/viewpost.jsp?thread=101605 |title=Five-minute Multimethods in Python}}</ref> with a simplified syntax: <syntaxhighlight lang="python"> @multimethod(Asteroid, Asteroid) def collide(a, b): """Behavior when asteroid hits a asteroid.""" # ...define new behavior... @multimethod(Asteroid, Spaceship) def collide(a, b): """Behavior when asteroid hits a spaceship.""" # ...define new behavior... # ... define other multimethod rules ... </syntaxhighlight> and then it goes on to define the multimethod decorator. The PEAK-Rules package provides multiple dispatch with a syntax similar to the above example.<ref>{{cite web |title=PEAK-Rules 0.5a1.dev |url=https://pypi.python.org/pypi/PEAK-Rules |website = Python Package Index |access-date=21 March 2014}}</ref> It was later replaced by PyProtocols.<ref>{{cite web |title=PyProtocols |url=http://peak.telecommunity.com/protocol_ref/module-protocols.html |website = Python Enterprise Application Kit |access-date=26 April 2019}}</ref> The Reg library also supports multiple and predicate dispatch.<ref>{{cite web |title=Reg |url=https://reg.readthedocs.io/en/latest/ |website = Read the docs |access-date=26 April 2019}}</ref> With the introduction of [[Python syntax and semantics#Function annotations|type hints]], multiple dispatch is possible with even simpler syntax. For example, using [https://github.com/beartype/plum plum-dispatch],<syntaxhighlight lang="python"> from plum import dispatch @dispatch def collide(a: Asteroid, b: Asteroid): """Behavior when asteroid hits a asteroid.""" # ...define new behavior... @dispatch def collide(a: Asteroid, b: Spaceship): """Behavior when asteroid hits a spaceship.""" # ...define new behavior... # ...define further rules... </syntaxhighlight> === Emulating multiple dispatch === ==== C ==== C does not have dynamic dispatch, so it must be implemented manually in some form. Often an enum is used to identify the subtype of an object. Dynamic dispatch can be done by looking up this value in a [[function pointer]] [[branch table]]. Here is a simple example in C: <syntaxhighlight lang="c"> typedef void (*CollisionCase)(void); void collision_AA(void) { /* handle Asteroid-Asteroid collision */ }; void collision_AS(void) { /* handle Asteroid-Spaceship collision */ }; void collision_SA(void) { /* handle Spaceship-Asteroid collision */ }; void collision_SS(void) { /* handle Spaceship-Spaceship collision*/ }; typedef enum { THING_ASTEROID = 0, THING_SPACESHIP, THING_COUNT /* not a type of thing itself, instead used to find number of things */ } Thing; CollisionCase collisionCases[THING_COUNT][THING_COUNT] = { {&collision_AA, &collision_AS}, {&collision_SA, &collision_SS} }; void collide(Thing a, Thing b) { (*collisionCases[a][b])(); } int main(void) { collide(THING_SPACESHIP, THING_ASTEROID); } </syntaxhighlight> With the C Object System library,<ref>{{Cite web | url=https://github.com/CObjectSystem/COS |title=C Object System: A framework that brings C to the level of other high level programming languages and beyond: CObjectSystem/COS |website=[[GitHub]] |date=2019-02-19}}</ref> C does support dynamic dispatch similar to CLOS. It is fully extensible and does not need any manual handling of the methods. Dynamic message (methods) are dispatched by the dispatcher of COS, which is faster than Objective-C. Here is an example in COS: <syntaxhighlight lang="c"> #include <stdio.h> #include <cos/Object.h> #include <cos/gen/object.h> // classes defclass (Asteroid) // data members endclass defclass (Spaceship) // data members endclass // generics defgeneric (_Bool, collide_with, _1, _2); // multimethods defmethod (_Bool, collide_with, Asteroid, Asteroid) // deal with asteroid hitting asteroid endmethod defmethod (_Bool, collide_with, Asteroid, Spaceship) // deal with asteroid hitting spaceship endmethod defmethod (_Bool, collide_with, Spaceship, Asteroid) // deal with spaceship hitting asteroid endmethod defmethod (_Bool, collide_with, Spaceship, Spaceship) // deal with spaceship hitting spaceship endmethod // example of use int main(void) { OBJ a = gnew(Asteroid); OBJ s = gnew(Spaceship); printf("<a,a> = %d\n", collide_with(a, a)); printf("<a,s> = %d\n", collide_with(a, s)); printf("<s,a> = %d\n", collide_with(s, a)); printf("<s,s> = %d\n", collide_with(s, s)); grelease(a); grelease(s); } </syntaxhighlight> ==== C++ ==== {{As of|2021}}, [[C++]] natively supports only single dispatch, though adding multi-methods (multiple dispatch) was proposed by [[Bjarne Stroustrup]] (and collaborators) in 2007.<ref>{{Cite web|last=|first=|date=2007-03-11|title=Report on language support for Multi-Methods and Open-Methods for C ++|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2216.pdf|quote=Multiple dispatch – the selection of a function to be invoked based on the dynamic type of two or more arguments – is a solution to several classical problems in object-oriented programming.|archive-url=|archive-date=|access-date=|website=}}</ref> The methods of working around this limit are analogous: use either the [[visitor pattern]], dynamic cast or a library: <syntaxhighlight lang="cpp"> // Example using run time type comparison via dynamic_cast struct Thing { virtual void collideWith(Thing& other) = 0; }; struct Asteroid : Thing { void collideWith(Thing& other) { // dynamic_cast to a pointer type returns NULL if the cast fails // (dynamic_cast to a reference type would throw an exception on failure) if (auto asteroid = dynamic_cast<Asteroid*>(&other)) { // handle Asteroid-Asteroid collision } else if (auto spaceship = dynamic_cast<Spaceship*>(&other)) { // handle Asteroid-Spaceship collision } else { // default collision handling here } } }; struct Spaceship : Thing { void collideWith(Thing& other) { if (auto asteroid = dynamic_cast<Asteroid*>(&other)) { // handle Spaceship-Asteroid collision } else if (auto spaceship = dynamic_cast<Spaceship*>(&other)) { // handle Spaceship-Spaceship collision } else { // default collision handling here } } }; </syntaxhighlight> or pointer-to-method lookup table: <syntaxhighlight lang="cpp"> #include <cstdint> #include <typeinfo> #include <unordered_map> class Thing { protected: Thing(std::uint32_t cid) : tid(cid) {} const std::uint32_t tid; // type id typedef void (Thing::*CollisionHandler)(Thing& other); typedef std::unordered_map<std::uint64_t, CollisionHandler> CollisionHandlerMap; static void addHandler(std::uint32_t id1, std::uint32_t id2, CollisionHandler handler) { collisionCases.insert(CollisionHandlerMap::value_type(key(id1, id2), handler)); } static std::uint64_t key(std::uint32_t id1, std::uint32_t id2) { return std::uint64_t(id1) << 32 | id2; } static CollisionHandlerMap collisionCases; public: void collideWith(Thing& other) { auto handler = collisionCases.find(key(tid, other.tid)); if (handler != collisionCases.end()) { (this->*handler->second)(other); // pointer-to-method call } else { // default collision handling } } }; class Asteroid: public Thing { void asteroid_collision(Thing& other) { /*handle Asteroid-Asteroid collision*/ } void spaceship_collision(Thing& other) { /*handle Asteroid-Spaceship collision*/} public: Asteroid(): Thing(cid) {} static void initCases(); static const std::uint32_t cid; }; class Spaceship: public Thing { void asteroid_collision(Thing& other) { /*handle Spaceship-Asteroid collision*/} void spaceship_collision(Thing& other) { /*handle Spaceship-Spaceship collision*/} public: Spaceship(): Thing(cid) {} static void initCases(); static const std::uint32_t cid; // class id }; Thing::CollisionHandlerMap Thing::collisionCases; const std::uint32_t Asteroid::cid = typeid(Asteroid).hash_code(); const std::uint32_t Spaceship::cid = typeid(Spaceship).hash_code(); void Asteroid::initCases() { addHandler(cid, cid, CollisionHandler(&Asteroid::asteroid_collision)); addHandler(cid, Spaceship::cid, CollisionHandler(&Asteroid::spaceship_collision)); } void Spaceship::initCases() { addHandler(cid, Asteroid::cid, CollisionHandler(&Spaceship::asteroid_collision)); addHandler(cid, cid, CollisionHandler(&Spaceship::spaceship_collision)); } int main() { Asteroid::initCases(); Spaceship::initCases(); Asteroid a1, a2; Spaceship s1, s2; a1.collideWith(a2); a1.collideWith(s1); s1.collideWith(s2); s1.collideWith(a1); } </syntaxhighlight> The ''YOMM2'' library<ref name="yomm2">[https://github.com/jll63/yomm2 yomm2], Fast, Orthogonal Open Multi-Methods for C++ by Jean-Louis Leroy.</ref> provides a fast, orthogonal implementation of open multimethods. The syntax for declaring open methods is inspired by a proposal for a native C++ implementation. The library requires that the user registers all the classes used as virtual arguments (and their sub-classes), but does not require any modifications to existing code. Methods are implemented as ordinary inline C++ functions; they can be overloaded and they can be passed by pointer. There is no limit on the number of virtual arguments, and they can be arbitrarily mixed with non-virtual arguments. The library uses a combination of techniques (compressed dispatch tables, collision free integer hash table) to implement method calls in constant time, while mitigating memory usage. Dispatching a call to an open method with a single virtual argument takes only 15–30% more time than calling an ordinary virtual member function, when a modern optimizing compiler is used. The Asteroids example can be implemented as follows: <syntaxhighlight lang="cpp"> #include <yorel/yomm2/keywords.hpp> #include <memory> class Thing { public: virtual ~Thing() {} }; class Asteroid : public Thing { }; class Spaceship : public Thing { }; register_classes(Thing, Spaceship, Asteroid); declare_method(void, collideWith, (virtual_<Thing&>, virtual_<Thing&>)); define_method(void, collideWith, (Thing& left, Thing& right)) { // default collision handling } define_method(void, collideWith, (Asteroid& left, Asteroid& right)) { // handle Asteroid-Asteroid collision } define_method(void, collideWith, (Asteroid& left, Spaceship& right)) { // handle Asteroid-Spaceship collision } define_method(void, collideWith, (Spaceship& left, Asteroid& right)) { // handle Spaceship-Asteroid collision } define_method(void, collideWith, (Spaceship& left, Spaceship& right)) { // handle Spaceship-Spaceship collision } int main() { yorel::yomm2::update_methods(); std::unique_ptr<Thing> a1(std::make_unique<Asteroid>()), a2(std::make_unique<Asteroid>()); std::unique_ptr<Thing> s1(std::make_unique<Spaceship>()), s2(std::make_unique<Spaceship>()); // note: types partially erased collideWith(*a1, *a2); // Asteroid-Asteroid collision collideWith(*a1, *s1); // Asteroid-Spaceship collision collideWith(*s1, *a1); // Spaceship-Asteroid collision collideWith(*s1, *s2); // Spaceship-Spaceship collision return 0; } </syntaxhighlight> Stroustrup mentions in ''The Design and Evolution of C++'' that he liked the concept of multimethods and considered implementing it in C++ but claims to have been unable to find an efficient sample implementation (comparable to virtual functions) and resolve some possible type ambiguity problems. He then states that although the feature would still be nice to have, that it can be approximately implemented using [[double dispatch]] or a type based lookup table as outlined in the C/C++ example above so is a low priority feature for future language revisions.<ref>{{cite book |last=Stroustrup |first=Bjarne |title=The Design and Evolution of C++ |publisher=Addison Wesley |location=Indianapolis, IN, U.S.A |year=1994 |chapter=Section 13.8 |isbn=978-0-201-54330-8|bibcode=1994dec..book.....S }}</ref> ==== D ==== {{As of|2021}}, as do many other object-oriented programming languages, [[D (programming language)|D]] natively supports only single dispatch. However, it is possible to emulate open multimethods as a library function in D. The ''openmethods'' library<ref name="openmethods">[https://github.com/jll63/methods.d openmethods], Open Multi-Methods for D by Jean-Louis Leroy.</ref> is an example. <syntaxhighlight lang="d"> // Declaration Matrix plus(virtual!Matrix, virtual!Matrix); // The override for two DenseMatrix objects @method Matrix _plus(DenseMatrix a, DenseMatrix b) { const int nr = a.rows; const int nc = a.cols; assert(a.nr == b.nr); assert(a.nc == b.nc); auto result = new DenseMatrix; result.nr = nr; result.nc = nc; result.elems.length = a.elems.length; result.elems[] = a.elems[] + b.elems[]; return result; } // The override for two DiagonalMatrix objects @method Matrix _plus(DiagonalMatrix a, DiagonalMatrix b) { assert(a.rows == b.rows); double[] sum; sum.length = a.elems.length; sum[] = a.elems[] + b.elems[]; return new DiagonalMatrix(sum); } </syntaxhighlight> ==== Java ==== In a language with only single dispatch, such as [[Java (programming language)|Java]], multiple dispatch can be emulated with multiple levels of single dispatch: [[File:UML class Java single dispatch.svg|UML class Java single dispatch.svg]] <syntaxhighlight lang="java"> interface Collideable { void collideWith(final Collideable other); /* These methods would need different names in a language without method overloading. */ void collideWith(final Asteroid asteroid); void collideWith(final Spaceship spaceship); } class Asteroid implements Collideable { public void collideWith(final Collideable other) { // Call collideWith on the other object. other.collideWith(this); } public void collideWith(final Asteroid asteroid) { // Handle Asteroid-Asteroid collision. } public void collideWith(final Spaceship spaceship) { // Handle Asteroid-Spaceship collision. } } class Spaceship implements Collideable { public void collideWith(final Collideable other) { // Call collideWith on the other object. other.collideWith(this); } public void collideWith(final Asteroid asteroid) { // Handle Spaceship-Asteroid collision. } public void collideWith(final Spaceship spaceship) { // Handle Spaceship-Spaceship collision. } } </syntaxhighlight> Run time <code>instanceof</code> checks at one or both levels can also be used. == Support in programming languages == === Primary paradigm === * [[Julia (programming language)|Julia]]<ref name="juliaManual">{{cite web |url=http://docs.julialang.org/en/release-0.4/manual/methods/ |title=Methods |publisher=Julialang |work=The Julia Manual |access-date=11 May 2014 |archive-url=https://web.archive.org/web/20160717192005/http://docs.julialang.org/en/release-0.4/manual/methods/ |archive-date=17 July 2016 |url-status=dead }}</ref> === Supporting general multimethods === * [[C Sharp 4.0|C# 4.0]]<ref>{{cite web |url=http://blogs.msdn.com/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx |title=Multimethods in C# 4.0 With 'Dynamic' |access-date=2009-08-20 }}</ref> * [[Cecil (programming language)|Cecil]]<ref>{{cite web |url=http://www.cs.washington.edu/research/projects/cecil/www/cecil.html |title=Cecil Language |access-date=2008-04-13 }}</ref> * [[Clojure]]<ref>{{cite web |url=http://clojure.org/multimethods |title=Multimethods in Clojure |access-date=2008-09-04 }}</ref> * [[Common Lisp]] (via the [[Common Lisp Object System]])<ref>{{cite book |last=Steele |first=Guy L. |title=Common LISP: The Language |publisher=Digital Press |location=Bedford, MA, U.S.A |year=1990 |chapter=28 |isbn=978-1-55558-041-4 |chapter-url=https://books.google.com/books?id=8Hr3ljbCtoAC }}</ref> * [[Dylan (programming language)|Dylan]]<ref>{{cite web |url=http://www.opendylan.org/books/drm/Background_and_Goals |title=Background and Goals |access-date=2008-04-13 }}</ref> * [[Emacs Lisp]] (via [https://www.gnu.org/software/emacs/manual/html_node/elisp/Generic-Functions.html cl-defmethod]) * [[Fortress (programming language)|Fortress]]<ref>{{cite web |url=http://research.sun.com/projects/plrg/Publications/fortress.1.0.pdf |title=The Fortress Language Specification, Version 1.0 |access-date=2010-04-23 |archive-url=https://web.archive.org/web/20130120063452/http://research.sun.com/projects/plrg/Publications/fortress.1.0.pdf |archive-date=2013-01-20 |url-status=dead }}</ref> * [[Groovy (programming language)|Groovy]]<ref>{{cite web |url=http://blogs.oracle.com/sundararajan/entry/multimethods_in_groovy |title=Multimethods in Groovy |access-date=2008-04-13 }}</ref> * [[Lasso (programming language)|Lasso]]<ref>{{cite web |url=http://lassoguide.com/language/methods.html#multiple-dispatch |title=Methods – LassoGuide 9.2 |access-date=2014-11-11 }}</ref><ref>{{cite web |url=http://nice.sourceforge.net/visitor.html |title=Visitor Pattern Versus Multimethods |access-date=2008-04-13 }}</ref> * [[Nim (programming language)|Nim]], up to v0.19.x (from v0.20.0 it is necessary to pass a compiler flag)<ref>{{cite web |url=https://nim-lang.org/docs/manual.html#methods-multiminusmethods |title=Nim Manual: Multi-methods |access-date=2022-05-03 }}</ref> * [[Raku (programming language)|Raku]]<ref>{{cite web |url=http://dev.perl.org/perl6/faq.html |title=Perl 6 FAQ |access-date=2008-04-13 }}</ref> * [[R (programming language)|R]]<ref>{{cite web |url=http://developer.r-project.org/howMethodsWork.pdf |title=How S4 Methods Work |access-date=2008-04-13 }}</ref> * [[Seed7]]<ref>{{cite web |url=http://seed7.sourceforge.net/manual/objects.htm#multiple_dispatch |title=Multiple Dispatch in Seed7 |access-date=2011-04-23 }}</ref> * [[TADS]]<ref>{{cite web |url=http://tads.org/t3doc/doc/sysman/multmeth.htm |title=TADS 3 System Manual |access-date=2012-03-19 }}</ref> * [[Visual Basic (.NET)]] (VB.NET)<ref>{{cite web |url=https://www.infoq.com/news/2007/06/VB-Multiple-Dispatch/ |title=VB.Net Multiple Dispatch |access-date=2020-03-31 }}</ref> via late binding, also via [[.Net DLR]]<ref>{{cite web |url=https://www.red-gate.com/simple-talk/dotnet/visual-studio/the-new-features-in-c4-0/ |title=New Features in C#4.0 and VB.Net 10.0 |date=4 November 2010 |access-date=2020-03-31 }}</ref> * [[Wolfram Language]]<ref>{{cite web |url=https://www.wolfram.com/language/for-experts/ |title=Notes for Programming Language Experts |access-date=2016-08-21 }} </ref> via symbolic pattern matching * [http://www.eclipse.org/xtend Xtend]<ref>{{cite web |url=https://www.eclipse.org/xtend/documentation/202_xtend_classes_members.html#polymorphic-dispatch |title=Multiple dispatch }}</ref> === Via extensions === * Any [[.NET]] framework language (via the library [http://www.codeplex.com/multimethods MultiMethods.NET]) * [[C (programming language)|C]] (via the library [https://github.com/CObjectSystem/COS C Object System]) * [[C Sharp (programming language)|C#]] (via the library [https://code.google.com/p/multimethod-sharp/ multimethod-sharp]) * [[C++]] (via the library [https://github.com/jll63/yomm2 yomm2], [https://github.com/IgorNikitin/multimethods multimethods] and [https://github.com/Hectarea1996/omm omm]) * [[D (programming language)|D]] (via the library [https://github.com/jll63/methods.d openmethods]) * [[Factor (programming language)|Factor]] (via the standard [http://docs.factorcode.org/content/vocab-multi-methods.html multimethods vocabulary]) * [[Java (programming language)|Java]] (using the extension [http://multijava.sourceforge.net/ MultiJava]) * [[JavaScript]] (via package [https://www.npmjs.com/package/@arrows/multimethod @arrows/multimethod]) * [[Perl]] (via the module [https://metacpan.org/module/Class::Multimethods Class::Multimethods]) * [[Python (programming language)|Python]] (via [https://pypi.python.org/pypi/PEAK-Rules PEAK-Rules], [https://web.archive.org/web/20050409082546/http://peak.telecommunity.com/ RuleDispatch], [https://web.archive.org/web/20050309230813/http://gnosis.cx/download/gnosis/magic/multimethods.py gnosis.magic.multimethods], [http://sourceforge.net/projects/pymultimethods/ PyMultimethods], [https://multiple-dispatch.readthedocs.org/en/latest/ multipledispatch], or [https://github.com/beartype/plum plum-dispatch]) * [[Racket (programming language)|Racket]] (via [https://docs.racket-lang.org/multimethod/index.html multimethod-lib]) * [[Ruby (programming language)|Ruby]] (via the library [https://rubygems.org/gems/multi/ The Multiple Dispatch Library] and [https://rubygems.org/gems/multimethod Multimethod Package] and [https://rubygems.org/gems/vlx-multi/ Vlx-Multimethods Package]) * [[Scheme (programming language)|Scheme]] (via e.g. [http://community.schemewiki.org/?tiny-clos TinyCLOS]) * [[TypeScript]] (via package [https://www.npmjs.com/package/@arrows/multimethod @arrows/multimethod]) == See also == * [[Predicate dispatch]] == References == {{Reflist}} == External links == * {{cite conference |last1=Stroustrup |first1=Bjarne |last2=Solodkyy |first2=Yuriy |last3=Pirkelbauer |first3=Peter |title=Open Multi-Methods for C++ |conference=ACM 6th International Conference on Generative Programming and Component Engineering |year=2007 |url=http://www.stroustrup.com/multimethods.pdf }} *{{cite web |url=https://docs.racket-lang.org/multimethod/ |title=Dynamic multiple dispatch |website=docs.racket-lang.org |access-date=2018-03-12}} [[Category:Method (computer programming)]] [[Category:Polymorphism (computer science)]] [[Category:Programming language comparisons]] <!-- Hidden categories below --> [[Category:Articles with example C code]] [[Category:Articles with example C++ code]] [[Category:Articles with example C Sharp code]] [[Category:Articles with example D code]] [[Category:Articles with example Java code]] [[Category:Articles with example JavaScript code]] [[Category:Articles with example Julia code]] [[Category:Articles with example Lisp (programming language) code]] [[Category:Articles with example Perl code]] [[Category:Articles with example Python (programming language) code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:As of
(
edit
)
Template:Citation
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite conference
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Polymorphism
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Webarchive
(
edit
)