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

Content added Content deleted
(added MiniScript example)
Line 901: Line 901:


Here, the two 'variables' can be seen under the single heading 'f'. And of course all of this is done at runtime.
Here, the two 'variables' can be seen under the single heading 'f'. And of course all of this is done at runtime.

=={{header|MiniScript}}==

If the name of the variable to add is known at compile time, then this is just standard class construction:
<lang MiniScript>empty = {}
empty.foo = 1</lang>

If the name of the variable to add is itself in a variable, then instead of dot syntax, use normal indexing:

<lang MiniScript>empty = {}
varName = "foo"
empty[varName] = 1</lang>

Either method results in a perfectly ordinary class or instance (there is no technical distinction between these in MiniScript), which can be used as usual by subsequent code.


=={{header|Morfa}}==
=={{header|Morfa}}==