This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:reflection [2007/04/27 14:47] stijnm Added |
at:tutorial:reflection [2010/11/16 16:32] tvcutsem |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | ||
- | ====== 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 reflective model of AmbientTalk is based on **mirrors**, | ||
- | |||
- | ===== Mirrors ===== | ||
- | As we have already mentioned in the introduction, | ||
- | |||
- | < | ||
- | 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() }; | ||
- | </ | ||
- | |||
- | The code excerpt presented above uses the mirror to // | ||
- | |||
- | 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. | ||
- | |||
- | < | ||
- | def invokeUserMethod(object) { | ||
- | def userInput := read: (system.readln()); | ||
- | // This example assumes that the user typed a single symbol | ||
- | (reflect: object).invoke(object, | ||
- | }; | ||
- | </ | ||
- | |||
- | This part of the tutorial has provided a basic feeling of how AmbientTalk' | ||
- | |||
- | ===== 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 could define objects which allow for the dynamic addition of unknown methods and fields. First of all, we need to create a mirror instance which we can use to create new objects from. This can be performed using the '' | ||
- | |||
- | < | ||
- | def dynamicExtensionMirror := mirror: { | ||
- | def doesNotUnderstand(selector) { | ||
- | system.println(" | ||
- | system.println(" | ||
- | def input := system.readln(); | ||
- | if: !( "" | ||
- | def definition := read: input; | ||
- | eval: definition in: base; | ||
- | } else: { | ||
- | super^doesNotUnderstand(selector); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | This mirror overrides the default implementation of the meta-operation '' | ||
- | |||
- | < | ||
- | def mirage := object: { | ||
- | def m() { self.x }; | ||
- | } mirroredBy: dynamicExtensionMirror; | ||
- | </ | ||
- | |||
- | When invoking the method '' | ||
- | |||
- | < | ||
- | Note that the use of '' | ||
- | </ | ||
- | |||
- | 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' | ||