# What's so mutable about \`Symbol\`s?

**URL:** <https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094>\
**Category:** General Usage\
**Created:** [January 1, 2018, 3:52pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094 "2018-01-01T15:52:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 1, 2018, 3:52pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/1 "2018-01-01T15:52:25Z")

</div>

May someone explain the rationale behind making `Symbol`s mutable? They seem to have no fields to mutate!

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 1, 2018, 4:07pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/2 "2018-01-01T16:07:04Z")

</div>

Where did you see something that said `Symbol`s are mutable?  
They are very much immutable, and (as an implementation detail) are interned as well, so that you can compare for equality simply by checking the pointers, instead of doing an O(n) comparison.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 1, 2018, 4:09pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/3 "2018-01-01T16:09:06Z")

</div>

```julia
julia> isimmutable(:x)
false

```

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 1, 2018, 4:19pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/4 "2018-01-01T16:19:08Z")

</div>

That’s really strange!

Maybe you should raise an issue about that. `isimmutable` returns `true` for a `String` pre-v0.6, but false after, and other things that also are immutable by convention and in the documentation that are implemented with a mutable struct (`BigInt`, `BigFloat`), also return false.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [January 1, 2018, 4:29pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/5 "2018-01-01T16:29:16Z")

</div>

Symbols are fairly special. They are one of the few types defined in C and have special representation and special handling pretty much everywhere in the compiler, so whether the runtime thinks it’s mutable or not doesn’t really matter. That said, returning true here would be more accurate. I’ll try flipping it and see what happens.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 1, 2018, 4:38pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/6 "2018-01-01T16:38:56Z")

</div>

`isbits` also returns false btw.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [January 1, 2018, 4:44pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/7 "2018-01-01T16:44:13Z")

</div>

That’s fine, it’s not. In particular, Symbols have an identity independent of their contents (the identities are linked, because Symbols are interned, but if you somehow had a different Symbol with identical contents, you’d have a problem). In particular this means that Symbols are always referenced by pointer.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 1, 2018, 5:10pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/8 "2018-01-01T17:10:38Z")

</div>

Any particular reason that `String` (which used to return `true` in v0.5) should return `false`?  
Same thing for `BigInt` and `BigFloat`, which (at least by convention, same as `String`) are considered immutable?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [January 1, 2018, 5:34pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/9 "2018-01-01T17:34:06Z")

</div>

[https://github.com/JuliaLang/julia/pull/25349](https://github.com/JuliaLang/julia/pull/25349)

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [January 1, 2018, 5:36pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/10 "2018-01-01T17:36:50Z")

</div>

Well, the reflection functions answer questions about the system, not convention. It is in fact possible to mutate a BigInt and there’s valid reasons to want to do so. Regarding `String`, I hope that with [https://github.com/JuliaLang/julia/pull/25241](https://github.com/JuliaLang/julia/pull/25241) we can truthfully answer `true` here.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 1, 2018, 5:42pm UTC](https://discourse.julialang.org/t/whats-so-mutable-about-symbol-s/8094/11 "2018-01-01T17:42:08Z")

</div>

> [@Keno](#):
>
> Well, the reflection functions answer questions about the system, not convention. It is in fact possible to mutate a BigInt and there’s valid reasons to want to do so.

I am aware of those reasons, but I think though that there are better ways of wrapping the underlying GMP (`BigInt`) and MPFR (`BigFloat`)'s structures, such that you would have faster, truly immutable types for those, with all the benefits that entails (like with the decimals in `DecFP.jl`), and have a separate clean wrapper for those cases where you really do want a reference to a mutable `BigInt` or `BigFloat` (for example, to be able to sum up a vector of `BigFloat` values.

> [@Keno](#):
>
> Regarding String, I hope that with [https://github.com/JuliaLang/julia/pull/25241](https://github.com/JuliaLang/julia/pull/25241) we can truthfully answer true here.

I really like that, and I’ll add an extension to `isimmutable` now for my `Strs` package that returns `true`, as I’d already made sure those could not be aliased to a mutable vector.
