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
Function object
(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!
== In Eiffel == In the [[Eiffel (programming language)|Eiffel]] software development method and language, operations and objects are seen always as separate concepts. However, the [[Eiffel (programming language)#Agents|agent]] mechanism facilitates the modeling of operations as runtime objects. Agents satisfy the range of application attributed to function objects, such as being passed as arguments in procedural calls or specified as callback routines. The design of the agent mechanism in Eiffel attempts to reflect the object-oriented nature of the method and language. An agent is an object that generally is a direct instance of one of the two library classes, which model the two types of routines in Eiffel: <code>PROCEDURE</code> and <code>FUNCTION</code>. These two classes descend from the more abstract <code>ROUTINE</code>. Within software text, the language keyword <code>agent</code> allows agents to be constructed in a compact form. In the following example, the goal is to add the action of stepping the gauge forward to the list of actions to be executed in the event that a button is clicked. <syntaxhighlight lang="eiffel"> my_button.select_actions.extend (agent my_gauge.step_forward) </syntaxhighlight> The routine <code>extend</code> referenced in the example above is a feature of a class in a graphical user interface (GUI) library to provide [[event-driven programming]] capabilities. In other library classes, agents are seen to be used for different purposes. In a library supporting data structures, for example, a class modeling linear structures effects [[universal quantification]] with a function <code>for_all</code> of type <code>BOOLEAN</code> that accepts an agent, an instance of <code>FUNCTION</code>, as an argument. So, in the following example, <code>my_action</code> is executed only if all members of <code>my_list</code> contain the character '!': <syntaxhighlight lang="eiffel"> my_list: LINKED_LIST [STRING] ... if my_list.for_all (agent {STRING}.has ('!')) then my_action end ... </syntaxhighlight> When agents are created, the arguments to the routines they model and even the target object to which they are applied can be either ''closed'' or left ''open''. Closed arguments and targets are given values at agent creation time. The assignment of values for open arguments and targets is deferred until some point after the agent is created. The routine <code>for_all</code> expects as an argument an agent representing a function with one open argument or target that conforms to actual generic parameter for the structure (<code>STRING</code> in this example.) When the target of an agent is left open, the class name of the expected target, enclosed in braces, is substituted for an object reference as shown in the text <code>agent {STRING}.has ('!')</code> in the example above. When an argument is left open, the question mark character ('?') is coded as a placeholder for the open argument. The ability to close or leave open targets and arguments is intended to improve the flexibility of the agent mechanism. Consider a class that contains the following procedure to print a string on standard output after a new line: <syntaxhighlight lang="eiffel"> print_on_new_line (s: STRING) -- Print `s' preceded by a new line do print ("%N" + s) end </syntaxhighlight> The following snippet, assumed to be in the same class, uses <code>print_on_new_line</code> to demonstrate the mixing of open arguments and open targets in agents used as arguments to the same routine. <syntaxhighlight lang="eiffel"> my_list: LINKED_LIST [STRING] ... my_list.do_all (agent print_on_new_line (?)) my_list.do_all (agent {STRING}.to_lower) my_list.do_all (agent print_on_new_line (?)) ... </syntaxhighlight> This example uses the procedure <code>do_all</code> for linear structures, which executes the routine modeled by an agent for each item in the structure. The sequence of three instructions prints the strings in <code>my_list</code>, converts the strings to lowercase, and then prints them again. Procedure <code>do_all</code> iterates across the structure executing the routine substituting the current item for either the open argument (in the case of the agents based on <code>print_on_new_line</code>), or the open target (in the case of the agent based on <code>to_lower</code>). Open and closed arguments and targets also allow the use of routines that call for more arguments than are required by closing all but the necessary number of arguments: <syntaxhighlight lang="eiffel"> my_list.do_all (agent my_multi_arg_procedure (closed_arg_1, ?, closed_arg_2, closed_arg_3) </syntaxhighlight> The Eiffel agent mechanism is detailed in the [http://www.ecma-international.org/publications/standards/Ecma-367.htm Eiffel ISO/ECMA standard document].
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)