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
Last revision Both sides next revision
at:tutorial:reflection [2009/11/30 16:54]
dharnie *prog->soft
at:tutorial:reflection [2010/11/16 15:59]
tvcutsem fixed
Line 85: Line 85:
 ===== Mirages ===== ===== Mirages =====
  
-Extending the AmbientTalk core language involves adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer can redefine the programming language's default semantics of the MOP, by means of mirages.+Extending the AmbientTalk core language requires adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer can redefine the programming language's default semantics of the MOP, by means of mirages.
  
 As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the ''defaultMirror'', which encapsulates the default semantics. By selectively overriding this mirror's methods, we can make small adjustments to the language, e.g. as follows: As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the ''defaultMirror'', which encapsulates the default semantics. By selectively overriding this mirror's methods, we can make small adjustments to the language, e.g. as follows:
Line 91: Line 91:
 <code> <code>
 def createTracingMirror(baseObject) { def createTracingMirror(baseObject) {
-  extend: defaultMirror with: {+  extend: defaultMirror.new(baseObject) with: {
     def invoke(slf, invocation) {     def invoke(slf, invocation) {
       system.println("invoked "+invocation.selector+" on "+baseObject);       system.println("invoked "+invocation.selector+" on "+baseObject);
Line 100: Line 100:
 </code> </code>
  
-The primitive ''mirror:'' is syntactic sugar for extending from the ''defaultMirror'', allowing the above example to be rewritten as:+The primitive ''mirror:'' is syntactic sugar for creating a new prototype that extends from the ''defaultMirror''. Thuswe could have also defined the tracing mirror as:
  
 <code> <code>
-def createTracingMirror(baseObject) { +def TracingMirror := mirror: { 
-  mirror: { +  def invoke(slf, invocation) { 
-    def invoke(slf, invocation) { +    system.println("invoked "+invocation.selector+" on "+self.base); 
-      system.println("invoked "+invocation.selector+" on "+baseObject); +    super^invoke(slf, invocation);
-      super^invoke(slf, invocation); +
-    }+
   }   }
 } }
Line 121: Line 119:
 </code> </code>
  
-In the code above, the closure passed to ''mirroredBy:'' is a //mirror construction closure//. This closure is applied to a new, empty mirage object and it is expected that it returns a new mirror that reflects upon this mirage. When the mirror is constructed, the object initialization closure is executed. The picture below gives an overview of the different objects involved in the actor.+In the code above, the closure passed to ''mirroredBy:'' is a //mirror construction closure//. This closure is applied to a new, empty mirage object and it is expected that it returns a new mirror that reflects upon this mirage. When the mirror is constructed, the object initialization closure is executed.
  
-{{:at:tutorial:meta-2.jpg|:at:tutorial:meta-2.jpg}}+Another alternative is to just pass a mirror prototype to ''object:mirroredBy:'', like so:
  
 +<code>
 +def mirage := object: {
 +  def foo() { 42 };
 +} mirroredBy: TracingMirror;
 +</code>
  
-When invoking the method ''m'' on the mirage, ''invoke'' will be invoked on the tracing mirror, causing the following behaviour:+The AmbientTalk VM will then call ''new'' on the ''TracingMirror'' prototype to create a new mirror for the given ''mirage'' object. 
 + 
 +When invoking the method ''foo'' on the mirage, ''invoke'' will be invoked on the tracing mirror, causing the following behaviour:
  
 <code> <code>
-> mirage.m(); +> mirage.foo(); 
-invoked on <obj:6276614{foo,...}>+invoked foo on <obj:6276614{foo,...}>
 >> 42 >> 42
 </code> </code>
 +
 +The picture below gives an overview of the different objects involved in the actor.
 +
 +{{:at:tutorial:meta-2.jpg|:at:tutorial:meta-2.jpg}}
  
 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 language construct which is conceived as a reflective extension to the language is that of future-type message passing. Futures 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 language construct which is conceived as a reflective extension to the language is that of future-type message passing. Futures are discussed in the next chapter of this tutorial.
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem