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

m
m (Moved jq entry into correct alphabetical order.)
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 3.4.x:
<lang elena>import extensions.;
 
class Extender :: BaseExtender
{
prop object prop foo :: theField.;
constructor new : anObject(object)
[{
theObject := anObject.object
]}
}
 
public program()
{
[
var anObjectobject := 234.;
// extending an object with a field
anObjectobject := Extender new Extender(anObjectobject).;
 
anObject object.foo := "bar".;
 
console .printLine(anObjectobject,".foo=",anObject object.foo).;
 
console .readChar()
]}</lang>
{{out}}
<pre>
Anonymous user