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
(section)
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!
=== 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>
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)