# \`Set\`s inside \`Set\`s = dangerous

**URL:** <https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669>\
**Category:** General Usage\
**Tags:** data\_structures\
**Created:** [April 30, 2024, 7:51pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669 "2024-04-30T19:51:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 30, 2024, 7:51pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/1 "2024-04-30T19:51:08Z")

</div>

A colleague discovered the following behaviour. I presume it’s known by those who know, but it doesn’t seem to be documented?

```julia
julia> s1 = Set{Int}([1]);

julia> s2 = Set{Int}([1, 2]);

julia> S = Set([s1, s2]);

       # mutate s1 so that it becomes identical to s2:

julia> push!(s1, 2);

julia> S
Set{Set{Int64}} with 2 elements:
  Set([2, 1])
  Set([2, 1])

# S does not realize that s1 and s2 are identical

```

Python and C++ both error when you try to create a set of sets, for this reason.  
Shouldn’t Julia do the same?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 30, 2024, 8:00pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/2 "2024-04-30T20:00:39Z")

</div>

This is true of not just `Set{T}` where `T<:Set`, but **all** mutable objects whose hashes change with mutation — and this includes arrays. Even “worse” are dictionaries, where you lose access to any keys that you mutate.

~~I’m pretty sure this is documented, but perhaps it’s in Dict’s docs.~~ Edit: hrm, yes, these would be good cautions to add to both `Set` and `Dict`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 30, 2024, 8:08pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/3 "2024-04-30T20:08:16Z")

</div>

Sets of sets aren’t the issue, it’s mutating anything that’s used as a hash key—dict or set.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 30, 2024, 8:11pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/4 "2024-04-30T20:11:36Z")

</div>

IMO the python approach is really annoying. It’s perfectly possible to use a `Set{Set}` safely (as long as you don’t mutate the inner `Set`s and having to use a different immutable data-structure just to put something into a collection is something that I would get annoyed by every few months when I was using python more.

---

<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:** [April 30, 2024, 8:27pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/5 "2024-04-30T20:27:18Z")

</div>

It’s useful to have a way to encode “you can’t safely mutate this”, to communicate that fact to other programmers (including myself, later or in other parts of the program). Takafumi was working on a freeze/melt interface I thought was good.

> **[GitHub - tkf/Mutabilities.jl](https://github.com/tkf/Mutabilities.jl)**
>
> Contribute to tkf/Mutabilities.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 30, 2024, 8:36pm UTC](https://discourse.julialang.org/t/set-s-inside-set-s-dangerous/113669/6 "2024-04-30T20:36:36Z")

</div>

> [@mbauman](#):
>
> ~~I’m pretty sure this is documented, but perhaps it’s in Dict’s docs.~~

It is, from Julia 1.11: [Warn about mutating keys in Dict docstring. by GunnarFarneback · Pull Request #51171 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/51171). That PR does not cover `Set`.
