Confusion about immutability (of named tuples)

Consider the code below:

a = 42
b = "Julia confuses me"

namedTuple =(; a, b)
println(ismutable(namedTuple)) # gives false

c = pi/2
namedTuple = (;namedTuple..., c) # Why is this permitted...
println(ismutable(namedTuple)) # gives false

namedTuple.c = exp(1) # ... While this gives ERROR: LoadError: setfield!: immutable struct of type NamedTuple cannot be changed

In particular I do not get why I can change namedTuple through addition of a new element, but cannot change existing.

Additionally, how do I make namedTuple mutable?


NamedTuple is an immutable type - you cannot make it mutable.

It’s because you’re not changing the existing object, but creating a new one and binding that to the same name, which replaces the object the name is referring to.

You can think of variable names as labels of an object - assigning an object to a variable means labelling that object with that variable name.