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