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

Content added Content deleted
(→‎LOLCODE: Add demonstration)
Line 463: Line 463:
e.foo = 1
e.foo = 1
e["bar"] = 2 // name specified at runtime</lang>
e["bar"] = 2 // name specified at runtime</lang>

=={{header|LOLCODE}}==
<tt>BUKKIT</tt>s (the all-purpose container type) can be added to at any point during execution, and the <tt>SRS</tt> operator permits the creation of identifiers from strings. This program and its output demonstrate both by prompting the user for a name and a value, modifying the object accordingly, and then printing the value of the new variable.
<lang LOLCODE>HAI 1.3

I HAS A object ITZ A BUKKIT
I HAS A name, I HAS A value

IM IN YR interface
VISIBLE "R U WANTIN 2 (A)DD A VAR OR (P)RINT 1? "!
I HAS A option, GIMMEH option

option, WTF?
OMG "A"
VISIBLE "NAME: "!, GIMMEH name
VISIBLE "VALUE: "!, GIMMEH value
object HAS A SRS name ITZ value, GTFO
OMG "P"
VISIBLE "NAME: "!, GIMMEH name
VISIBLE object'Z SRS name
OIC
IM OUTTA YR interface

KTHXBYE</lang>
Example run:
<pre>R U WANTIN 2 (A)DD A VAR OR (P)RINT 1? A
NAME: foo
VALUE: 42
R U WANTIN 2 (A)DD A VAR OR (P)RINT 1? P
NAME: foo
42</pre>


=={{header|Lua}}==
=={{header|Lua}}==