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 |
ELENA 4.x: |
||
<lang elena>import extensions |
<lang elena>import extensions; |
||
class Extender |
class Extender : BaseExtender |
||
{ |
{ |
||
object |
prop object foo; |
||
constructor |
constructor(object) |
||
{ |
|||
theObject := |
theObject := object |
||
} |
|||
} |
} |
||
public program |
public program() |
||
{ |
|||
[ |
|||
var |
var object := 234; |
||
// extending an object with a field |
// extending an object with a field |
||
object := new Extender(object); |
|||
object.foo := "bar"; |
|||
console |
console.printLine(object,".foo=",object.foo); |
||
console |
console.readChar() |
||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |