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!
==== 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.
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)