User Tools

Site Tools


at:tutorial:actors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
at:tutorial:actors [2007/04/07 18:30] tvcutsemat:tutorial:actors [2007/04/07 20:10] tvcutsem
Line 309: Line 309:
 ==== Actor Mirrors ==== ==== Actor Mirrors ====
  
-explain: mirror factory, message creation, message sendinginstall+An actor in AmbientTalk is primarily a //host// for regular objects. It is equipped with a message queue to receive asynchronous messages sent to one of its objects. The mirrors on these objects have corresponding meta-level operations such as ''send'' and ''receive'' that can be overridden to customise e.g. message sending on a per-object basis. 
 + 
 +Some operations, such as creating and sending asynchronous messages are useful to reify at the //actor level//. With such a reification, the programmer could override the way messages are sent by any object from within an actor. The mirror on an actor that reifies these operations is bound to the ''actor'' variable, defined at top-level. Evaluating ''actor'' returns the mirror on the current actor. It defines operations such as ''createMessage'' and ''send'' which can be explicitly invoked, or even overridden by the programmer. 
 + 
 +Overriding the actor's metaobject protocol can be done by //installing// a new protocol object. This is done by invoking the ''install:'' method on ''actor''. The following code installs a new MOP that logs any messages sent by any object in the current actor: 
 + 
 +<code> 
 +def oldmirror := actor.install: (extend: actor with: { 
 +  def send(msg) { 
 +    log(msg); 
 +    super^send(msg); 
 +  }; 
 +}); 
 +</code> 
 + 
 +Notice that, in this example, the new metaobject protocol is an extension of the old protocol. This enables it to invoke its parent's behaviour simply by means of a super-send. Note also that the ''install:'' primitive returns the previously installed mirror. This is useful for when an actor mirror should only temporarily be installed. The old mirror can later be re-installed. 
 + 
 +For a good use case of actor mirrorssee the ''at/lang/futures.at'' file in the system library. This file implements future-type message passing. It uses a custom actor mirror that overrides ''createMessage'' -- which is invoked whenever an asynchronous message is created -- to attach a future to the message. 
 + 
 +Other methods that can be overridden are ''require'' and ''provide''which reify the export and discovery of objects, and ''createMirror'', which is invoked by the ''reflect:'' primitive. By overriding this //factory method//, it becomes possible to easily customize the behaviour of mirrors defined on local objects.
  
 ==== Nesting Actors ==== ==== Nesting Actors ====
  
 lexical scoping rules for nested actors lexical scoping rules for nested actors
at/tutorial/actors.txt · Last modified: 2020/02/05 21:26 by elisag