[self-interest] Adding method slots dynamically

Jecel Assumpcao Jr jecel at merlintec.com
Thu Jul 8 18:35:34 UTC 2004

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

More information about the Self-interest mailing list