Add a variable to a class instance at runtime: Difference between revisions

Content added Content deleted
m (Moved jq entry into correct alphabetical order.)
Line 327: Line 327:
ELENA does not support adding a field at run-time but it can be simulated with the help of a mix-in.
ELENA does not support adding a field at run-time but it can be simulated with the help of a mix-in.


ELENA 3.4:
ELENA 4.x:
<lang elena>import extensions.
<lang elena>import extensions;


class Extender :: BaseExtender
class Extender : BaseExtender
{
{
object prop foo :: theField.
prop object foo;
constructor new : anObject
constructor(object)
[
{
theObject := anObject.
theObject := object
]
}
}
}


public program
public program()
{
[
var anObject := 234.
var object := 234;
// extending an object with a field
// extending an object with a field
anObject := Extender new(anObject).
object := new Extender(object);


anObject foo := "bar".
object.foo := "bar";


console printLine(anObject,".foo=",anObject foo).
console.printLine(object,".foo=",object.foo);


console readChar
console.readChar()
]</lang>
}</lang>
{{out}}
{{out}}
<pre>
<pre>