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

Content added Content deleted
(Pascal entry)
Line 340: Line 340:
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 4.x:
ELENA 6.x:
<syntaxhighlight lang="elena">import extensions;
<syntaxhighlight lang="elena">import extensions;


class Extender : BaseExtender
class Extender : BaseExtender
{
{
prop object foo;
object foo : prop;
constructor(object)
constructor(object)
{
{
theObject := object
this object := object
}
}
}
}


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


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


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


console.readChar()
console.readChar()
}</syntaxhighlight>
}</syntaxhighlight>
{{out}}
{{out}}