# Iterating through types of a Union in a type-stable manner

**URL:** <https://discourse.julialang.org/t/iterating-through-types-of-a-union-in-a-type-stable-manner/58285>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [March 31, 2021, 10:58am UTC](https://discourse.julialang.org/t/iterating-through-types-of-a-union-in-a-type-stable-manner/58285 "2021-03-31T10:58:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [March 31, 2021, 10:58am UTC](https://discourse.julialang.org/t/iterating-through-types-of-a-union-in-a-type-stable-manner/58285/1 "2021-03-31T10:58:48Z")

</div>

Hi,

I have the following:

```julia
union_types(x::Union) = union_types(x.a, x.b)
union_types(a::Union, b::Type) = (union_types(a)..., b)
union_types(a::Type, b::Type) = (a, b)
union_types(x::Type) = (x,)
# For completeness
union_types(a::Type, b::Union) = (a, union_types(b)...)

```

which is a way to get the types composing a Union as a Tuple. However, it is not type-stable:

![image](https://global.discourse-cdn.com/julialang/original/3X/c/7/c79e306670dba9d43220c25769ea45db39e612d0.png)

Is it possible to get, or iterate over, the Types composed in a Union in a type-stable manner, **without knowing beforehand how many types will be**? The user provide us with a Union of types, so we cannot make assumptions on the total number of types beforehand.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 31, 2021, 11:41am UTC](https://discourse.julialang.org/t/iterating-through-types-of-a-union-in-a-type-stable-manner/58285/2 "2021-03-31T11:41:42Z")

</div>

Take a look at this thread and other threads that are referenced in it: [Macro to write function with many conditionals](https://discourse.julialang.org/t/macro-to-write-function-with-many-conditionals/51616)

There is no simple answer, although there are many possibilities.

---

<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:** [March 31, 2021, 12:03pm UTC](https://discourse.julialang.org/t/iterating-through-types-of-a-union-in-a-type-stable-manner/58285/3 "2021-03-31T12:03:29Z")

</div>

> [@Datseris](#):
>
> The user provide us with a Union of types, so we cannot make assumptions on the total number of types beforehand.

I think you should try to find another API that doesn’t require you to use internals like this. Manipulating types like this is not really how the language is supposed to be used and you will always have pushbacks from various parts if you go this way.
