User Tools

Site Tools


at:tutorial:reflection

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
at:tutorial:reflection [2007/05/03 16:31] stimbermat:tutorial:reflection [2007/07/13 10:15] jorge
Line 1: Line 1:
-<note>This tutorial is still under heavy construction.</note> 
- 
 ====== Reflective Programming ====== ====== Reflective Programming ======
  
Line 8: Line 6:
  
 ===== Mirrors ===== ===== Mirrors =====
-As we have already mentioned in the introduction, AmbientTalk uses a mirror-based architecture to provide reflective access to its objects. The basic principle of a mirror-based architecture is that all reflective facilities are encapsulated in a mirror object which provides reflective access to precisely one object, its reflectee. Moreover, the mirror of the object is not directly accessible as a slot of the object. Instead, a separate factory must be used to create mirrors, which allows the program to hand out different mirrors according to the dynamic call chain, the requesting object etc. The factory can be used implicitly using the ''reflect:'' primitive. Once a mirror has been created, it can be used for instance to produce a listing of an object's slots, as is exemplified below.+AmbientTalk uses a mirror-based architecture to provide reflective access to its objects. The basic principle of a mirror-based architecture is that all reflective facilities are encapsulated in a mirror object which provides reflective access to precisely one object, its reflectee. Moreover, the mirror of the object is not directly accessible as a slot of the object. Instead, a separate factory must be used to create mirrors, which allows the program to hand out different mirrors according to the dynamic call chain, the requesting object etc. The factory can be used implicitly using the ''reflect:'' primitive. Once a mirror has been created, it can be used for instance to produce a listing of an object's slots, as is exemplified below.
  
 <code> <code>
 def baseObject := object: { def baseObject := object: {
   def field := nil;   def field := nil;
-  def canonicalMethod() { nil } +  def canonicalMethod() { nil }; 
-  def keyworded: arg1 method: arg2 { nil }+  def keyworded: arg1 method: arg2 { nil };
 }; };
 def mirror := reflect: baseObject; def mirror := reflect: baseObject;
Line 49: Line 47:
     } else: {     } else: {
       super^doesNotUnderstand(selector);       super^doesNotUnderstand(selector);
-    } +    }; 
-  }+  };
 } }
 </code> </code>
Line 69: Line 67:
  
 Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk's main domain of application). An example of a language construct which is conceived as a reflective extension to the language is for instance futures, which are discussed in the next chapter of this tutorial. Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk's main domain of application). An example of a language construct which is conceived as a reflective extension to the language is for instance futures, which are discussed in the next chapter of this tutorial.
 +
 +===== The Metaobject Protocol =====
 +
 +The Meta-Object Protocol of AmbientTalk can be divided into a series of independent protocols. Whereas the full semantics and signature of the meta-methods can be found in the [[http://prog.vub.ac.be/amop/at2langref/edu/vub/at/objects/Object.html|language reference]], this section provides an overview of the various protocols.
 +
 +The **Message Passing Protocol** consists of methods to deal with both synchronous and asynchronous message sending. It provides necessary hooks to intercept both the reception of asynchronous messages and the invocation of synchronous messages. Moreover, it provides a hook to intercept asynchronous messages being sent by the object, allowing the object to add additional metadata to the message. The ''invoke'' meta-method illustrated above is an example of a method belonging to this protocol.
 +
 +The **Object Passing Protocol** consists of two methods ''pass'' and ''resolve'', which allow an object to prescribe how it should be passed to other objects and how the object should subsequently be resolved upon arrival. The default semantics allow objects to be passed by copy if they are tagged with the ''Isolate'' type tag. Otherwise, objects are passed by handing out a far object reference.
 +
 +The **Slot Access and Modification Protocol** consists of operations which allow trapping both access and modification to slots. These operations are further refined based on whether they transitively search the dynamic or lexical parent chain. For instance, for the lookup of a variable, ''lookup'' traverses the lexical chain whereas ''select'' (which requires an additional receiver parameter) traverses the dynamic parent chain.
 +
 +The **Structural Access Protocol** consists of operations used list all available slots, get access to a first-class slot representation and to add new slots to an existing object. The ''listSlots'' meta-method used in previous examples is an element of this protocol.
 +
 +The **Instantiation Protocol** consists of the ''clone'' and ''newInstance'' methods, which are implictly called when using base-level code of the form ''clone: object'' and ''object.new(@args)'' respectively. In the default implementation, ''newInstance'' calls ''clone()'' to create a clone of the current object, and subsequently invokes the base-level ''init'' method with the supplied arguments on the cloned object.
 +
 +The **Relational Testing Protocol** consists of the methods ''isCloneOf'' and ''isRelatedTo'' which allow testing whether 2 objects are related through cloning or a combination of cloning and object extension. Note that these operators are not able to discern that objects were generated by the same constructor function or (by extension) the execution of identical code in different actors. 
 +
 +The **Type Testing Protocol** consists of the methods ''isTaggedAs'' and ''getTypeTags'', the former of which tests whether an object is transitively tagged with a particular type (i.e. this operation considers type tags of parent objects). ''getTypeTags'' on the other hand returns only the type tags which were explicitly attributed to this object (i.e. it does not return type tags attributed to the parent objects).
 +
 +The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the ''eval'' and ''quote'' meta-operations. The ''eval'' operation is called when evaluating a parsetree and typically returns the evaluated object itself, except for explicit parse-tree elements such as definition and method invocations. 
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem