Add a variable to a class instance at runtime: Difference between revisions
Content added Content deleted
(+ F#/Omit) |
|||
Line 279: | Line 279: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA does not support adding a |
ELENA does not support adding a field at run-time but it can be simulated with the help of a group object |
||
<lang elena># |
<lang elena>#define system. |
||
#class |
#class Extender |
||
{ |
{ |
||
#field |
#field theObject. |
||
#field theField. |
|||
# |
#constructor new : anObject |
||
[ |
[ |
||
theObject := anObject. |
|||
] |
] |
||
#method foo |
#method foo = theField. |
||
⚫ | |||
#method set &foo : aValue |
|||
⚫ | |||
theField := aValue. |
|||
⚫ | |||
#method => theObject. |
|||
} |
} |
||
#symbol |
#symbol program = |
||
[ |
[ |
||
#var anObject := 234. |
#var anObject := 234. |
||
// adding a field |
// adding a field |
||
anObject := anObject |
anObject := Extender new:anObject. |
||
⚫ | |||
anObject |
anObject set &foo:"bar". |
||
⚫ | |||
console << anObject << ".foo=" << anObject foo. |
|||
⚫ | |||
console readChar. |
|||
].</lang> |
].</lang> |
||