Hello,
How can I add dynamically a method slot to a self object? . I have: the method selector, the argument names and the body (source code) of a method, and I need (at runtime) to create a method from them and add it to an existing object.
Thanks for the help,
Oscar Lopez
On Thursday 08 July 2004 13:37, oscar_lopez_p wrote:
How can I add dynamically a method slot to a self object? .
First you need a mirror on the object you are interested in:
reflect: myObj
Now you can just add the slot with the name you want and a mirror on the value you want for that slot:
(reflect: myObj) at: 'selector' PutContents: mir
I have: the method selector, the argument names and the body (source code) of a method, and I need (at runtime) to create a method from them and add it to an existing object.
You can create a mirror on a method from a string:
(reflect: myObj) at: 'selector:And:' PutContents: ('| :arg1. :arg2. tmp <- 9 | (arg1*arg2)+tmp' parseObjectBody)
There are several variations on this theme. See methods in traits string (evaluation category) and in traits mirrors abstractMirror (programming category) for more options.
-- Jecel
Hi, there !
--- In self-interest@yahoogroups.com, Jecel Assumpcao Jr <jecel@m...> wrote:
First you need a mirror on the object you are interested in: You can create a mirror on a method from a string:
(reflect: myObj) at: 'selector:And:' PutContents: ('| :arg1. :arg2. tmp <- 9 | (arg1*arg2)+tmp' parseObjectBody)
I don't understand why you need a mirror object in order to add/delete/modify(?) methods or attributes (I suppose you need the same trick for attributes).
What's the role of the mirror, here ? Is it related to preformance ?
Thank you in advance,
Baltasar
"baltasarq" wrote:
--- In self-interest@yahoogroups.com, Jecel Assumpcao Jr <jecel@m...> wrote:
First you need a mirror on the object you are interested in: You can create a mirror on a method from a string:
(reflect: myObj) at: 'selector:And:' PutContents: ('| :arg1. :arg2. tmp <- 9 | (arg1*arg2)+tmp' parseObjectBody)
I don't understand why you need a mirror object in order to add/delete/modify(?) methods or attributes (I suppose you need the same trick for attributes).
What's the role of the mirror, here ? Is it related to preformance ?
It isn't a performance issue at all, but rather a matter of good object oriented design. The paper I mentioned last month goes into details about why mirrors are a good idea (http://bracha.org/mirrors.pdf) but here is a short answer:
Every object you create is "about" something. You might create an object called "circle" that is about circles on the screen, for example. It should know things like its diameter, its center and perhaps stuff about its border and interior color. The more we can make ourselfves believe that this actually is a circle, the better it will be to work with it.
Another context might require different things from our object: a debugger or program editor might want to know things like the number of slots it has, how many bytes it takes up in RAM and so on. An obvious solution is to add methods to this object (and probably every single object in the system as well!) so these applications can do their job. The problem with this is that the new behavior isn't at all circle-like. We are ruining the "this object is a circle" illusion (which we call the base-level programming domain) by mixing a "this object is a bunch of slots" illusion (which we call the meta-level programming domain).
An alternative design is to separate the meta-level stuff into a helper object. Since meta-level programming which changes the implementation level is called "reflection" we will name this helper object a "mirror". This separation allows a circle to be pure again - all it knows about is circleness. The mirror is also pretty close to pure, knowing only about reflection. Another advantage of this organization is that just a few kinds or mirrors are enough to deal with all objects in Self.
-- Jecel
self-interest@lists.selflanguage.org