at:tutorial:reflection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
at:tutorial:reflection [2007/07/18 16:30] – jorge | at:tutorial:reflection [2007/08/08 13:35] – Updated stijnm | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | < | ||
+ | |||
====== Reflective Programming ====== | ====== Reflective Programming ====== | ||
- | [[wp>Reflection|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**, | + | [[wp>Reflection_(computer_science)|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 [[http:// | The reflective model of AmbientTalk is based on [[http:// | ||
===== Mirrors ===== | ===== Mirrors ===== | ||
- | 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 '' | + | As we have already mentioned in the introduction, |
< | < | ||
- | def baseObject | + | >def mirrorOnOne |
- | def field := nil; | + | >>< |
- | def canonicalMethod() { nil }; | + | > |
- | def keyworded: arg1 method: | + | >> |
- | }; | + | <native method:<=>>, <native method:to:do:>, ...] |
- | def mirror | + | |
- | def slots := mirror.listSlots(); | + | |
- | slots.each: { | slot | system.println(slot) }; | + | |
</ | </ | ||
- | The code excerpt presented above uses the mirror to // | + | The code excerpt presented above uses the mirror to // |
- | In addition to allowing | + | When reflecting upon a user-defined object, we can observe that every object has some implictly defined methods and fields, |
< | < | ||
- | def invokeUserMethod(object) { | + | >def inspectable := object: { |
- | def userInput | + | def map(arg1, @restArgs) { restArgs.map(arg1); |
- | // This example assumes that the user typed a single symbol | + | >>< |
- | (reflect: | + | >def mirrorOnInspectable |
- | }; | + | >>< |
+ | > | ||
+ | >> | ||
+ | > | ||
+ | >> | ||
+ | < | ||
+ | >def method := mirrorOnInspectable.grabMethod(`map); | ||
+ | >>< | ||
+ | > | ||
+ | >> | ||
+ | </code> | ||
+ | |||
+ | Using a mirror on an object, it is possible to get access to a representation of the object' | ||
+ | |||
+ | In addition to allowing a program to reason about the structure of its objects, mirrors can also be used to perform operations such as method invocation in a first-class manner. The following example shows how to select all zero-argument methods whose name starts with '' | ||
+ | |||
+ | < | ||
+ | >def isTestMethod(meth) { | ||
+ | | ||
+ | { meth.parameters == [] } }; | ||
+ | >>< | ||
+ | >def retainTestMethods(obj) { | ||
+ | | ||
+ | | ||
+ | >>< | ||
+ | >def runTest(obj) { | ||
+ | | ||
+ | | ||
+ | >>< | ||
+ | > | ||
+ | ok | ||
+ | >>nil | ||
</ | </ | ||
Line 51: | Line 81: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | < | ||
This mirror overrides the default implementation of the meta-operation '' | This mirror overrides the default implementation of the meta-operation '' |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem