# Why are the items in Julia's Sets mutable, but there is no immutable version of the latter?

**URL:** https://discourse.julialang.org/t/why-are-the-items-in-julias-sets-mutable-but-there-is-no-immutable-version-of-the-latter/102898
**Category:** General Usage
**Tags:** question, set
**Created:** [August 17, 2023, 9:49am UTC](https://discourse.julialang.org/t/why-are-the-items-in-julias-sets-mutable-but-there-is-no-immutable-version-of-the-latter/102898 "2023-08-17T09:49:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ARCJ137442](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arcj137442/32/52279_2.png) [@ARCJ137442](https://discourse.julialang.org/u/ARCJ137442)
#### Post date: [August 17, 2023, 9:49am UTC](https://discourse.julialang.org/t/why-are-the-items-in-julias-sets-mutable-but-there-is-no-immutable-version-of-the-latter/102898/1 "2023-08-17T09:49:42Z")

</div>

## Background

1. First, clarify a point: a set in Julia is an “immutable type”, but in its definition (’ set.jl ‘), a set is represented as a “wrapper around a Dict”, and this “immutability” is represented as "you cannot change the dictionary (’ dict 'field) wrapped in it".
2. Secondly, according to the source code of Julia v1.9.1, the feature of Julia collection “element disorder does not repeat” comes from the disordered non-repeatability of its dictionary key.
3. In addition, in assembly, C and other languages that are closer to hardware, the length of the array cannot be changed, and its immutable version is similar to the Tuple in Julia.
4. In the case of immutable contents, the actual performance of immutable arrays is usually better than that of mutable arrays.

Based on the above background, the problem are as follows:

## Problem

Requirements: Use an immutable Set of elements, immutable number of elements, and want to outperform Julia’s built-in ‘set’ type

Conflict: `Set` in Julia is’ element variable ‘and’ element number variable ’

Solution attempts before asking questions:

1. When searching on Discourse, there was no Topic about “immutable set”
2. Try to write an type named of `ImmutableSet{T} <: AbstractSet` by containing a `Tuple` as the only field, and find that its performance is not as good as the built-in `Set`
3. In [GitHub - JuliaLang/julia: The Julia Programming Language](https://github.com/JuliaLang/julia), search on all Issues, not found with “immutable collections” about the Topic
4. Trying to search all GitHub repositories written in Julia, there is no repository related to “Immutable Set”

## My code of the trial on `ImmutableSet{T}`

```julia
struct ImmutableSet{T} <: Base.AbstractSet{T}
    tuple::Tuple{Vararg{T}}

    global _ImmutableSet(tuple::Tuple, T::Type) = new{T}(
        Tuple(unique(tuple))
    )
end

ImmutableSet{T}() where {T} = _ImmutableSet(Tuple{}(), T)
ImmutableSet{T}(s::ImmutableSet{T}) where {T} = @show ImmutableSet{T}(s.tuple)
ImmutableSet{T}(itr) where {T} = _ImmutableSet(Tuple{Vararg{T}}(itr), T)
ImmutableSet() = ImmutableSet{Any}()

ImmutableSet(itr) = _ImmutableSet(itr, Base.IteratorEltype(itr))
_ImmutableSet(itr, ::Base.HasEltype) = ImmutableSet{eltype(itr)}(itr)

function _ImmutableSet(itr, ::Base.EltypeUnknown)
    T = @Base.default_eltype(itr)
    (isconcretetype(T) || T === Union{}) || return grow_to!(ImmutableSet{T}(), itr)
    return ImmutableSet{T}(itr)
end

Base.empty(s::ImmutableSet{T}, ::Type{U}=T) where {T,U} = ImmutableSet{U}()

Base.isempty(s::ImmutableSet) = isempty(s.tuple)
Base.length(s::ImmutableSet) = length(s.tuple)
Base.in(x, s::ImmutableSet) = in(x, s.tuple)

Base.copy(s::ImmutableSet) = copymutable(s)
Base.copymutable(s::ImmutableSet{T}) where {T} = ImmutableSet{T}(s.tuple)

Base.iterate(s, i...) = iterate(s.tuple, i...)

function Base.show(io::IO, s::ImmutableSet)
    if Base.isempty(s)
        if Base.get(io, :typeinfo, Any) == Base.typeof(s)
            Base.print(io, "ImmutableSet()")
        else
            Base.show(io, Base.typeof(s))
            Base.print(io, "()")
        end
    else
        Base.print(io, "ImmutableSet(")
        Base.show_vector(io, s)
        Base.print(io, ')')
    end
end

# alias
const ISet::Type = ImmutableSet

# test
@show s = ISet()
@show s = ISet([1,2,3])
@show s = ISet((1,2,3))
@show s = ISet(Set([1,2,3]))
@show s = ISet(ISet([1,2,3]))
@show s = ISet(1=>2)
@show s = ISet(Dict(
    1=>2,
    2=>3,
))

@time for i in 1:0xff
    Set(j for j in 1:i)
end

@time for i in 1:0xff
    ImmutableSet(j for j in 1:i)
end

```

## Others

One of the related discussions about “immutable set”:

> [@In what way are Julia sets immutable?](https://discourse.julialang.org/t/in-what-way-are-julia-sets-immutable/49009):
>
> 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" "b…

---

<div class="post-metadata">

### Author: ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)
#### Post date: [August 17, 2023, 10:05am UTC](https://discourse.julialang.org/t/why-are-the-items-in-julias-sets-mutable-but-there-is-no-immutable-version-of-the-latter/102898/2 "2023-08-17T10:05:50Z")

</div>

[GitHub - noahbenson/Air.jl: Light-weight functional collections and utilities for Julia.](https://github.com/noahbenson/Air.jl) ?

> <https://github.com/JuliaCollections/FunctionalCollections.jl/issues/40>
>
> I would love Julia to have a high-performance, actively maintained immutable col…lections library, but it looks like this project isn't under active development.
> 
> Are there any plans for FunctionalCollections.jl going forward?
