[self-interest] Re: Adding method slots dynamically
Jecel Assumpcao Jr
jecel at merlintec.com
Tue Aug 16 14:42:14 UTC 2005
> --- In self-interest at yahoogroups.com, Jecel Assumpcao Jr
> <jecel at 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.
More information about the Self-interest