# Iterating over types in Union

**URL:** <https://discourse.julialang.org/t/iterating-over-types-in-union/111172>\
**Category:** General Usage\
**Created:** [March 5, 2024, 2:58am UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172 "2024-03-05T02:58:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 5, 2024, 2:58am UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172/1 "2024-03-05T02:58:04Z")

</div>

Is there a way (that doesn’t involve reaching into private details of Base/Core) to iterate over the types in a `Union`? In my case I know that it’ll only ever be a small union of concrete types, if that helps.

I’m writing a library to do REST API interaction and I have a `Dict` holding information on each endpoint. This includes a return type (Julia type) for that endpoint. The server returns JSON which is parsed into a `Dict` and then converted into the expected return type.

Except that exactly one of 200 endpoints can return two different types of data. So I have the return type as `Union{A, B}` and want to convert the returned `Dict` into either `A` or `B`, as appropriate.

Is it possible to write code something like this?

```julia
for type in return_types
    can_convert_to(type, returned_data) && return type(returned_data)
end

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 5, 2024, 3:19am UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172/2 "2024-03-05T03:19:55Z")

</div>

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

```

good enough? It does go into Base, but the function name doesn’t start with an `_` which is a little better.

---

<div class="post-metadata">

**Author:** ![ndortega](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndortega/32/37018_2.png) [@ndortega](https://discourse.julialang.org/u/ndortega)\
**Post date:** [March 5, 2024, 5:20am UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172/3 "2024-03-05T05:20:52Z")

</div>

I’d like to piggyback off of Dan’s answer - that’s exactly what I’d recommend as well. In my own package I’ve taken the same approach to parse & convert path parameters so I could pass them directly to a request handler.

Here’s the function with that union iteration:

> <https://github.com/OxygenFramework/Oxygen.jl/blob/v1.5.1/src/utilities/misc.jl#L120>

Here’s an example of it in action:

> <https://github.com/OxygenFramework/Oxygen.jl/blob/v1.5.1/src/core.jl#L533>

---

<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:** [March 5, 2024, 8:02am UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172/4 "2024-03-05T08:02:36Z")

</div>

> <https://github.com/JuliaLang/julia/issues/53193>
>
> Useful functionality like \`Base.typesplit\` or \`Base.promote\_union\` may be implem…ented by recursively mapping over the components of a \`Union\` object. I wonder if it would make sense to provide a public API so users could do that themselves. I guess either of these would be nice:
> 
> 1. document and support the \`a\` and \`b\` fields of a \`Union\`, or
> 
> 2. provide some higher level API similar to \`mapreduce\`, that would take a type instead of a collection, interpreting union components as collection elements

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 5, 2024, 1:07pm UTC](https://discourse.julialang.org/t/iterating-over-types-in-union/111172/5 "2024-03-05T13:07:22Z")

</div>

I feel heartened that you are using this in a package like Oxygen. Not ideal to be using undocumented functions but it’s probably the best option for me.
