at:tutorial:reflection
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
at:tutorial:reflection [2007/04/26 22:15] – created stijnm | at:tutorial:reflection [2007/04/27 12:58] – Added stijnm | ||
---|---|---|---|
Line 6: | Line 6: | ||
The reflective model of AmbientTalk is based on **mirrors**, | 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' |
at/tutorial/reflection.txt · Last modified: 2010/11/16 16:32 by tvcutsem