PLDB
Languages Features Calendar CSV Lists Blog About Sponsor Add Language
GitHub icon

Multiple Dispatch

Multiple Dispatch

Multiple Dispatch is a feature.

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. 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. Multiple dispatch routes the dynamic dispatch to the implementing function or method using the combined characteristics of one or more arguments.. Read more on Wikipedia...


Languages without Multiple Dispatch include JavaScript, Elixir

Languages with Multiple Dispatch include Julia, Smalltalk, Common Lisp, Dylan

Example from Julia:
collide_with(x::Asteroid, y::Asteroid) = ... # deal with asteroid hitting asteroid collide_with(x::Asteroid, y::Spaceship) = ... # deal with asteroid hitting spaceship collide_with(x::Spaceship, y::Asteroid) = ... # deal with spaceship hitting asteroid collide_with(x::Spaceship, y::Spaceship) = ... # deal with spaceship hitting spaceship
Example from Common Lisp:
(defmethod collide-with ((x asteroid) (y asteroid)) ;; deal with asteroid hitting asteroid ) (defmethod collide-with ((x asteroid) (y spaceship)) ;; deal with asteroid hitting spaceship ) (defmethod collide-with ((x spaceship) (y asteroid)) ;; deal with spaceship hitting asteroid ) (defmethod collide-with ((x spaceship) (y spaceship)) ;; deal with spaceship hitting spaceship )
templates-feature.html 路 multiple-dispatch-feature.html 路 async-await-feature.html

View source

PLDB - Build the next great programming language 路 v2022 Day 28 Docs Build Acknowledgements Traffic Today Traffic Trends Mirrors GitHub feedback@pldb.com