# Confusion about immutability (of named tuples)

**URL:** <https://discourse.julialang.org/t/confusion-about-immutability-of-named-tuples/87355>\
**Category:** New to Julia\
**Created:** [September 16, 2022, 8:33am UTC](https://discourse.julialang.org/t/confusion-about-immutability-of-named-tuples/87355 "2022-09-16T08:33:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)\
**Post date:** [September 16, 2022, 8:33am UTC](https://discourse.julialang.org/t/confusion-about-immutability-of-named-tuples/87355/1 "2022-09-16T08:33:56Z")

</div>

Consider the code below:

```julia
a = 42
b = "Julia confuses me"

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

c = pi/2
namedTuple = (;namedTuple..., c) # Why is this permitted...
println(namedTuple)
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?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 16, 2022, 8:37am UTC](https://discourse.julialang.org/t/confusion-about-immutability-of-named-tuples/87355/2 "2022-09-16T08:37:23Z")

</div>

> [@DanDoe](#):
>
> Additionally, how do I make `namedTuple` mutable?

> **[GitHub - MasonProtter/MutableNamedTuples.jl: Basically what it says on the tin](https://github.com/MasonProtter/MutableNamedTuples.jl)**
>
> Basically what it says on the tin. Contribute to MasonProtter/MutableNamedTuples.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 16, 2022, 8:37am UTC](https://discourse.julialang.org/t/confusion-about-immutability-of-named-tuples/87355/3 "2022-09-16T08:37:28Z")

</div>

> [@DanDoe](#):
>
> Additionally, how do I make `namedTuple` mutable?

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

> [@DanDoe](#):
>
> In particular I do not get why I can change `namedTuple` through addition of a new element, but cannot change existing.

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.
