Agent

From Liberty Eiffel Wiki
Revision as of 13:44, 20 November 2013 by Cadrian (talk | contribs)
Jump to navigation Jump to search

In Eiffel, the notion of agent makes it possible to describe and manipulate computation parts (i.e. operations) like ordinary data. Operations may be partially described, may be passed as ordinary data and may have their execution delayed. Agents are very convenient for many purposes, such as going through data structures and implementing call-backs in graphical libraries.

See http://www.jot.fm/issues/issue_2004_04/article7/ — some parts are reused here.

Overview

The agent mechanism gives the Eiffel object-oriented language the ability to handle operations, or commands, as such, like in functional programming languages.

Agents are a new type of objects that allow to store code to be executed and data in an object, named the agent. In Eiffel, four types of agents exist: an abstract (deferred) type ROUTINE, and three concrete types PROCEDURE, FUNCTION and PREDICATE. The following figure shows their inheritance relationship:

Agents-fig1.png

Agents are a way of storing operations for later execution. They are objects. As such, they can be stored, compared to Void, or passed around to other software components. The operation stored in the agent may then be executed whenever the component decides. The most common uses of agents comprise delayed calls, multiple calls (on different values), lazy evaluation, and so on.


Inline agents

Inline agents are closures: they may access the arguments and local variables defined in their outside feature (called outside variables).

The usage limits are:

  • names must be unique (i.e. shadowing an outside variable is not allowed)
  • Result is not accessible to inline agents; one must use another local variable (rationale: if the agent is a function, what would Result refer to?)
  • access is read-only: you cannot assign to an outside local variable (and, of course, to an argument, but that's standard Eiffel anyway)

The technical limits are:

  • beware of automatically allocated pointers for outside local variables, especially if the GC is not used. There is no such problem for arguments since their value cannot change throughout the execution of the outside feature
  • expanded arguments are twinned in the inline agent context (of course)