In this tutorial we will implement Rosetta code sample - Add a variable to a class instance at run-time.
Though ELENA does not allow directly adding a field / method to the object it is possible dynamically to override any object with a help of the redirect method.
Let’s declare a wrapper class which will bind our class instance with a variable.
The code is simple: we declare a field and two methods accessing it: get&foo and set&foo: and an initializing constructor.
Now let’s declare a redirect method:
This method will redirect any unhandled message to the field - theObject. As a result we effectively dynamically override the object (so called horizontal inheritance).
Now let’s test the code:
The output will be the following:
The full tutorial code is below: