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 revision Previous revision
Next revision
Previous revision
at:tutorial:reflection [2007/04/27 12:58]
stijnm Added
at:tutorial:reflection [2010/11/16 16:32]
tvcutsem
Line 1: Line 1:
-<note>This tutorial is still under heavy construction.</note> 
-====== Reflective Programming ====== 
  
-Reflection is an integral part of the AmbientTalk programming language. Through the use of reflection, the core language can be extended with both programming support as well as new language constructs. Both examples require a different kind of reflective access. The introduction of programming support (e.g. to visualise AmbientTalk objects) relies on **introspection**, the ability for a program to inspect an reason about parts of its own state. This particular flavour of reflection is quite popular and is available in most contemporary programming languages. AmbientTalk goes beyond introspection and also allows objects to supply alternative semantics for the default meta-level operations. This particular form of reflection, called **intercession**, allows enriching AmbientTalk from within the language itself.  
- 
-The reflective model of AmbientTalk is based on **mirrors**, meta-level objects which allow one to reflect on an objects state and behaviour. How to create such mirrors and how they can be used is demonstrated in the first part of the tutorial. The second part of the tutorial showcases how to construct mirages, objects which override the default meta-level operations with custom behaviour. This tutorial concludes with a brief overview of the meta-level operations which are offered by AmbientTalk 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. 
- 
-<code> 
-def baseObject := object: { 
-  def field := nil; 
-  def canonicalMethod() { nil } 
-  def keyworded: arg1 method: arg2 { nil } 
-}; 
-def mirror := reflect: baseObject; 
-def slots := mirror.listSlots(); 
-slots.each: { | slot | system.println() }; 
-</code> 
- 
-The code excerpt presented above uses the mirror to //introspect// on an object and uses the ''listSlots'' meta-method to obtain a collection of the slots contained in this object, to wit ''field'', ''field:='', ''canonicalMethod'', ''keyworded:method:'' and the primitive slots ''=='', ''new'' and ''init'' which are implicitly present in every AmbientTalk object.  
- 
-In addition to allowing a program to reason about the structure of its objects, mirrors can also be used to write operations such as message sending in a first-class manner. The following example uses this power to invoke a zero-argument method, whose name is specified at runtime by requesting input from the user.  
- 
-<code> 
-def invokeUserMethod(object) { 
-  def userInput := read: (system.readln()); 
-  // This example assumes that the user typed a single symbol 
-  (reflect: object).invoke(object, userInput, []); 
-}; 
-</code> 
- 
-This part of the tutorial has provided a basic feeling of how AmbientTalk's default mirrors can be created and the kind of power they offer. The default mirrors offer a much wider range of capabilities than those presented in this section, however. A complete overview of all meta-operations will be presented in the final chapter of this tutorial. 
at/tutorial/reflection.txt ยท Last modified: 2010/11/16 16:32 by tvcutsem