# Union!() seems to be type-unstable, is this by design?

**URL:** <https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 11, 2026, 3:01pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747 "2026-08-11T15:01:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [August 11, 2026, 3:01pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747/1 "2026-08-11T15:01:14Z")

</div>

I’m not new to Julia, but am new to using Sets. The following MWE puzzles me (the autosearch for related topics reference some 2017 queries which aren’t relevant, AFAICS):

```julia-auto
julia> x = Set((1,2)); y = Set(('a', 'b')); union!(y,x)
ERROR: TypeError: 2 is not a valid key for type Char
Stacktrace:
 [1] setindex!(h::Dict{Char, Nothing}, v0::Nothing, key0::Int64)
   @ Base ./dict.jl:348
 [2] push!(s::Set{Char}, x::Int64)
   @ Base ./set.jl:137
 [3] union!(s::Set{Char}, itr::Set{Int64})
   @ Base ./abstractset.jl:106
 [4] top-level scope
   @ REPL[272]:1

julia> x = Set{Any}((1,2)); y = Set{Any}(('a', 'b')); union!(y,x)
Set{Any} with 4 elements:
  2
  'a'
  'b'
  1

```

Should I have expected this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 11, 2026, 3:06pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747/2 "2026-08-11T15:06:53Z")

</div>

> [@Henri\_Laurie](#):
>
> Should I have expected this?

What in particular is confusing for you?

---

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [August 11, 2026, 3:50pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747/3 "2026-08-11T15:50:18Z")

</div>

When Julia choosed type, the types are a mismatch (I didn’t realise that Set elements have all to be of the same type). But had I known, I would have expected promotion, even up to Any.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 11, 2026, 3:55pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747/4 "2026-08-11T15:55:57Z")

</div>

`union!(y,x)` means to update `y` to include the elements of `x`. But this only works if the set type of `y` can actually contain those elements (and you cannot change the type of an existing object). Maybe you wanted to use `union` (without a `!`):

```julia-auto
julia> x = Set((1,2)); y = Set(('a', 'b')); union(y,x)
Set{Any} with 4 elements:
  2
  'a'
  'b'
  1

```

---

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [August 11, 2026, 5:45pm UTC](https://discourse.julialang.org/t/union-seems-to-be-type-unstable-is-this-by-design/138747/5 "2026-08-11T17:45:55Z")

</div>

Thanks, I didn’t quite catch the type issues from reading the docs. There is an art to reading as well as writing them …
