# In what way are Julia sets immutable?

**URL:** https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009
**Category:** New to Julia
**Tags:** set
**Created:** [October 25, 2020, 11:20pm UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009 "2020-10-25T23:20:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Graph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_graph/32/18959_2.png) [@Mark\_Graph](https://discourse.julialang.org/u/Mark_Graph)
#### Post date: [October 25, 2020, 11:20pm UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/1 "2020-10-25T23:20:55Z")

</div>

If I create a set in Julia, then Julia will tell me that the set is immutable.

```
julia> pets = Set(["dog", "cat", "budgerigar"])
Set{String} with 3 elements:
  "cat"
  "budgerigar"
  "dog"

julia> ismutable(pets)
false

```

Nonetheless, I can modify the set in place.

```
julia> push!(pets, "orangutan")
Set{String} with 4 elements:
  "orangutan"
  "cat"
  "budgerigar"
  "dog"

```

And I can check that the set contents have changed.

```
julia> display(pets)
Set{String} with 4 elements:
  "orangutan"
  "cat"
  "budgerigar"
  "dog"

```

Similarly, I can delete from the set in place

```
julia> delete!(pets, "dog")
Set{String} with 3 elements:
  "orangutan"
  "cat"
  "budgerigar"

```

So my question is, in what way are sets immutable? In what way is their mutability different when compared with dictionaries?

```
julia> ismutable(Dict())
true

```

What am I not understanding?

---

<div class="post-metadata">

### Author: ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)
#### Post date: [October 25, 2020, 11:39pm UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/2 "2020-10-25T23:39:59Z")

</div>

Immutable types can have mutable properties.

Here is the definition of `Set`:

```julia
struct Set{T} <: AbstractSet{T}
    dict::Dict{T,Nothing}

    Set{T}() where {T} = new(Dict{T,Nothing}())
    Set{T}(s::Set{T}) where {T} = new(Dict{T,Nothing}(s.dict))
end

```

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [October 26, 2020, 1:17am UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/3 "2020-10-26T01:17:15Z")

</div>

I guess this might be the case for many types which wrap a mutable type. e.g. specialized arrays like `OffsetArrays`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 26, 2020, 8:55am UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/4 "2020-10-26T08:55:44Z")

</div>

I know this from before, that you can modify mutable fields wrapped in an immutable struct, but this still caught me by surprise.

Maybe it’s the fact that the `Dict` inside the `Set` is so ‘hidden’, you don’t even know it’s there. So `Set` _seems_ very much to be mutable. There’s some disconnect between the implementation detail that causes `Set` to be immutable, and the observed interface which is that `Set` _is_ mutable.

So `ismutable` isn’t the right way to figure out the behaviour of `Set`. Could there be a `Mutable` trait or something along those lines, that tell you about the _behaviour_ of a type, rather than its implementation?

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [October 26, 2020, 9:22am UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/5 "2020-10-26T09:22:47Z")

</div>

I think this should work

```julia
function truly_immutable(T)
    T === String && return true
    T === Symbol && return true
    body = T isa UnionAll ? T.body : T
    ft = fieldtypes(body)
    body.mutable && return false
    isempty(ft) && return true
    return all(truly_immutable, ft)
end
```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 26, 2020, 9:46am UTC](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009/6 "2020-10-26T09:46:32Z")

</div>

> [@DNF](#):
>
> So `ismutable` isn’t the right way to figure out the behaviour of `Set` .

Indeed it isn’t. It’s for querying a property with a very specific meaning in Julia’s type system — its docstring refers the reader to [mutable composite types](https://docs.julialang.org/en/v1/manual/types/#Mutable-Composite-Types).

> [@DNF](#):
>
> Could there be a `Mutable` trait

Technically it is possible, but it would make sense to think a bit about the application first, with examples. Personally I would prefer to design my code in a way that I don’t have to think about this for objects I do not “own”, and for the rest I know anyway.
