# Reflection API for Union types

**URL:** <https://discourse.julialang.org/t/reflection-api-for-union-types/81612>\
**Category:** General Usage\
**Created:** [May 24, 2022, 6:21pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612 "2022-05-24T18:21:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [May 24, 2022, 6:21pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/1 "2022-05-24T18:21:51Z")

</div>

Are there any methods to inspect `Union` types?

Ex:

```julia
julia> y::Union{Number,Nothing} = nothing

julia> typeof(y)
Nothing

```

1/ how to find out the fact that `y` is a `Union`?

2/ how to get the types supported by the `Union` (`Number` and `Nothing`)?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 24, 2022, 7:46pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/2 "2022-05-24T19:46:21Z")

</div>

Answer to 2) is `Base.uniontypes` though that isn’t exported

```julia
julia> Base.uniontypes(Union{Int, Nothing})
2-element Vector{Any}:
 Nothing
 Int64

```

You can find answers to a lot of questions about reflection in the [ExprTools.jl package, in `src/methods.jl`](https://github.com/invenia/ExprTools.jl/blob/3fdc2463648cbdf5df19cf29a808d119ea2cbcdf/src/method.jl#L177) because in the past i worked them out

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [May 24, 2022, 7:51pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/3 "2022-05-24T19:51:00Z")

</div>

While uniontypes is not exported, [it is only 3 lines long](https://github.com/JuliaLang/julia/blob/master/base/reflection.jl#L929). I just copied it when I needed it

```julia
_uniontypes(x::Union, ts) = (_uniontypes(x.a,ts); _uniontypes(x.b,ts); ts)
_uniontypes(@nospecialize(x), ts) = (push!(ts, x); ts)
uniontypes(@nospecialize(x)) = _uniontypes(x, Any[])

```

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [May 24, 2022, 8:17pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/4 "2022-05-24T20:17:51Z")

</div>

This is really cool, thanks for sharing! Though I’m now wary of using unexported methods as them and the APIs they rely on can be removed in minor Julia versions (I’ve been bitten by this quite a few times already).

---

<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:** [May 24, 2022, 8:19pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/5 "2022-05-24T20:19:49Z")

</div>

`y` isn’t a `Union`. Values don’t have non-concrete types.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [May 26, 2022, 5:33am UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/6 "2022-05-26T05:33:21Z")

</div>

> [@essenciary](#):
>
> I’m now wary of using unexported methods as them and the APIs they rely on can be removed in minor Julia versions

That’s why I copy/pasted the code into my own project, then it will always be there

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [May 26, 2022, 1:46pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/7 "2022-05-26T13:46:31Z")

</div>

Yes - that’s a very important point about Unions.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 26, 2022, 2:55pm UTC](https://discourse.julialang.org/t/reflection-api-for-union-types/81612/8 "2022-05-26T14:55:32Z")

</div>

Or any abstract type 🙂
