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
Double 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!
===== Visitor ===== The visitor for the classic Spaceship example also has a double-dispatch mechanism. <syntaxhighlight lang="eiffel" line highlight="35"> make -- Allow SPACESHIP objects to visit and move about in a universe. local l_universe: ARRAYED_LIST [SPACE_OBJECT] l_enterprise, l_excelsior: SPACESHIP do create l_enterprise.make_with_name ("Enterprise", "A-001") create l_excelsior.make_with_name ("Excelsior", "A-003") create l_universe.make (0) l_universe.force (l_enterprise) l_universe.force (create {ASTEROID}.make_with_name ("Rogue 1", "A-002")) l_universe.force (create {ASTEROID}.make_with_name ("Rogue 2", "A-003")) l_universe.force (l_excelsior) l_universe.force (create {ASTEROID}.make_with_name ("Rogue 3", "A-004")) l_universe.force (create {SPACESTATION}.make_with_name ("Deep Space 9", "A-005")) visit (l_enterprise, l_universe) l_enterprise.set_position ("A-002") visit (l_enterprise, l_universe) l_enterprise.set_position ("A-003") visit (l_enterprise, l_universe) l_enterprise.set_position ("A-004") l_excelsior.set_position ("A-005") visit (l_enterprise, l_universe) visit (l_excelsior, l_universe) l_enterprise.set_position ("A-005") visit (l_enterprise, l_universe) end feature {NONE} -- Implementation visit (a_object: SPACE_OBJECT; a_universe: ARRAYED_LIST [SPACE_OBJECT]) -- `a_object' visits `a_universe'. do across a_universe as ic_universe loop check attached {SPACE_OBJECT} ic_universe.item as al_universe_object then a_object.encounter_agent.call ([al_universe_object.sensor_data_agent]) end end end </syntaxhighlight> The double-dispatch can be seen in line #35, where two indirect agents are working together to provide two co-variant calls working in perfect polymorphic concert with each other. The `a_object' of the `visit' feature has an `encounter_agent' which is called with the sensor data of the `sensor_data_agent' coming from the `al_universe_object'. The other interesting part of this particular example is the SPACE_OBJECT class and its `encounter' feature:
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)