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

Content added Content deleted
(Added Go)
m (Moved jq entry into correct alphabetical order.)
Line 663: Line 663:
e.foo = 1
e.foo = 1
e["bar"] = 2 // name specified at runtime</lang>
e["bar"] = 2 // name specified at runtime</lang>

=={{header|jq}}==
jq's "+" operator can be used to add a key/value pair (or to add multiple key-value pairs) to an existing object at runtime, but
jq is a functional programming language, and objects themselves cannot be altered. Thus it may be helpful to introduce a variable, since the value of a variable can in effect be updated. For example:
<lang jq>{"a":1} as $a | ($a + {"b":2}) as $a | $a
</lang>Thus the value of $a has undergone the desired transition, that is, its final value is {"a":1, "b":2}.

A Javascript-like syntax can also be used to add (or update) a key, for example:<lang jq>$a|.c = 3
# or equivalently:
$a|.["c"] = 3</lang>


=={{header|Julia}}==
=={{header|Julia}}==
Line 748: Line 758:
Its value is 'rosetta'
Its value is 'rosetta'
</pre>
</pre>

=={{header|jq}}==
jq's "+" operator can be used to add a key/value pair (or to add multiple key-value pairs) to an existing object at runtime, but
jq is a functional programming language, and objects themselves cannot be altered. Thus it may be helpful to introduce a variable, since the value of a variable can in effect be updated. For example:
<lang jq>{"a":1} as $a | ($a + {"b":2}) as $a | $a
</lang>Thus the value of $a has undergone the desired transition, that is, its final value is {"a":1, "b":2}.

A Javascript-like syntax can also be used to add (or update) a key, for example:<lang jq>$a|.c = 3
# or equivalently:
$a|.["c"] = 3</lang>


=={{header|Lingo}}==
=={{header|Lingo}}==