Mathematica: TIP: How to change a value within an Association inline within a Module (like pass by reference)

Webel IT Australia promotes the amazing Mathematica tool and the powerful Wolfram Language and offers professional Mathematica services for computational computing and data analysis. Our Mathematica tips, issue tracking, and wishlist is offered here most constructively to help improve the tool and language and support the Mathematica user community.
DISCLAIMER: Wolfram Research does not officially endorse analysis by Webel IT Australia.
Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
Keywords

If you are an advocate of pure functional you may not find the following so appealing, but it's useful to at least know how this works.


Consider the following simple Association:


assoc = <|"a" -> 1, "b" -> 2|>
<|"a" -> 1, "b" -> 2|>
In the global scope the value of, say, "a", can be directly changed thus:

assoc["a"] = 3
3

assoc
<|"b" -> 2, "a" -> 3|>
But this does not work within a Module:

f1[assoc_, aNew_] := assoc["a"] = aNew

f1[assoc, 4]
Association::setps: <|b->2,a->1|> in the part assignment is not a symbol.
To make it work like pass-by-reference, apply a HoldFirst to f1:

SetAttributes[f1, HoldFirst]

f1[assoc, 4]
4

assoc
<|"b" -> 2, "a" -> 4|>
AppendTo[] can likewise be used to replace an Association value inline:

AppendTo[assoc, "a" -> 5]
<|"b" -> 2, "a" -> 5|>

assoc
<|"b" -> 2, "a" -> 5|>
But if you use it within a Module, you'll hit a similar error unless you apply a HoldFirst to it:

f2[assoc_, aNew_] := AppendTo[assoc, "a" -> aNew]
f2[assoc, 6]
AppendTo::rvalue: <|b->2,a->6|> is not a variable with a value, so its value cannot be changed.
But with HoldFirst applied it works fine:

SetAttributes[f2, HoldFirst]

f2[assoc, 6]
<|"b" -> 2, "a" -> 6|>

assoc
<|"b" -> 2, "a" -> 6|>
Relates to
Related notes
Related notes (backlinks)
Related snippets (extracts)
Visit also
Visit also (backlinks)
External links