# Unpack types in Union

**URL:** https://discourse.julialang.org/t/unpack-types-in-union/75803
**Category:** General Usage
**Tags:** question
**Created:** [February 4, 2022, 3:27pm UTC](https://discourse.julialang.org/t/unpack-types-in-union/75803 "2022-02-04T15:27:45Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 18, 2025, 12:24pm UTC](https://discourse.julialang.org/t/unpack-types-in-union/75803/21 "2025-09-18T12:24:09Z")

</div>

Sorry if I missed someone suggesting this somewhere, but this thread seemed to be mostly concerned with telling the OP to do something else. In case the OP or someone else actually does just need a function that turns a regular `Union` into a list of the union members, one way to do it without relying on internals would be like so:

```julia
function usplit(::Type{T}, acc=()) where {T}
    if T isa Union
        U, V = _usplit(T)
        (usplit(U)..., usplit(V)..., acc...)
    else
        (T, acc...)
    end
end
_usplit(::Type{Union{T, U}}) where {T, U} = (T, U)

```

then you can do e.g.

```julia-repl
julia> usplit(Union{Int, String, Array})
(Int64, String, Array)

```

This sort of thing should be efficiently hoisted to compile time whenever the union is statically known.

I chose to return a `Tuple` here because it has nice static properties, but if you need an `Array` you could just `collect` it at the end.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [September 18, 2025, 12:59pm UTC](https://discourse.julialang.org/t/unpack-types-in-union/75803/22 "2025-09-18T12:59:26Z")

</div>

> [@Mason](#):
>
> ```julia
> _usplit(::Type{Union{T, U}}) where {T, U} = (T, U)
> 
> ```

@Mason as discussed in the linked issue, your implementation is not correct, according to vtjnash.

> relies upon an implementation bug in subtyping and should be avoided in real code

I suppose one reason for that is that is that the semantics of what gets matched as `T`, and what gets matched as `U`, are not well defined.

For example, should `_usplit(Union{Int, Float32})` return `(Int, Float32)` or should it return `(Float32, Int)`? What about more complex types?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [September 18, 2025, 1:09pm UTC](https://discourse.julialang.org/t/unpack-types-in-union/75803/23 "2025-09-18T13:09:20Z")

</div>

I see, that’s unfortunate. I’m not sure why it would be so bad if the **order** was the only issue, but with such a laconic statement like the one from from the [linked issue](https://github.com/JuliaLang/julia/issues/53193), it’s hard to know if that’s the only potential problem.

[Previous page](https://discourse.julialang.org/t/unpack-types-in-union/75803.md?page=1)
