Jump to content

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

no edit summary
(→‎{{header|Tcl}}: Do it in an alternative way)
No edit summary
Line 116:
CL-USER 62 > (slot-value *an-instance* 'slot1)
23</pre>
 
=={{header|Falcon}}==
Classes and singleton objects have a fixed structure which cannot be changed during runtime. However falcon does have capability to add variables/functions at runtime with Prototype based objects. Below are two of the prototype objects that allow adding variables at runtime. These are arrays and dictionaries (hashes for the perl type out there).<br>
'''Array:'''
In this example we add a function (which prints out the content of the array) and a new value. While we are not technically adding a "variable", this example is presented to show similar type of functionality.
<lang falcon>
vect = [ 'alpha', 'beta', 'gamma' ]
vect.dump = function ()
for n in [0: self.len()]
> @"$(n): ", self[n]
end
end
vect += 'delta'
vect.dump()
</lang>
'''Dictionary:'''
In this example we create an object from a bless'ed dictionary. We assign a function (sub_func) to the variable 'add'. Additionally create a new variable called 'newVar' at runtime and assign a string to it.
<lang falcon>
function sub_func( value )
self['prop'] -= value
return self.prop
end
 
dict = bless( [
'prop' => 0,
'add' => function ( value )
self.prop += value
return self.prop
end ,
'sub' => sub_func
])
 
dict[ 'newVar' ] = "I'm Rich In Data"
</lang>
 
=={{header|Io}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.