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 variable at run-time but it can be simulated with the help of a group object
ELENA does not support adding a field at run-time but it can be simulated with the help of a group object
<lang elena>#subject foo.
<lang elena>#define system.


#class FieldContainer
#class Extender
{
{
#field theValue.
#field theObject.
#field theField.
#method foo'set : anObject
#constructor new : anObject
[
[
theValue := anObject.
theObject := anObject.
]
]
#method foo'get = theValue.
#method foo = theField.
#method set &foo : aValue
[
theField := aValue.
]
#method => theObject.
}
}


#symbol Program =
#symbol program =
[
[
#var anObject := 234.
#var anObject := 234.
// adding a field
// adding a field
anObject := anObject &= FieldContainer.
anObject := Extender new:anObject.

anObject foo'set:"bar".
anObject set &foo:"bar".

'program'Output << anObject << ".foo=" << anObject foo.
console << anObject << ".foo=" << anObject foo.

'program'input get.
console readChar.
].</lang>
].</lang>