# Transform \`Tuple{Union, NonUnion, Union, ...}\` to \`Union{Tuple{NonUnion, NonUnion, NonUnion, ...}, ...}\`

**URL:** <https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 3, 2020, 10:18am UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452 "2020-03-03T10:18:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 3, 2020, 10:18am UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452/1 "2020-03-03T10:18:54Z")

</div>

Hi all,

does there exist a function which can transform a tuple type of the schema `Tuple{Union, NonUnion, Union, ...}`, i.e. with Union types on the first level, such that the Union goes out and we have `Union{Tuple{NonUnion, NonUnion, NonUnion, ...}, Tuple{NonUnion, NonUnion, NonUnion, ...}, ...}`

For example

```julia
T = solutionfunction(Tuple{Union{Int, String}, Bool})
T == Union{Tuple{Int, Bool}, Tuple{String, Bool}}

```

---

<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:** [March 3, 2020, 1:49pm UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452/2 "2020-03-03T13:49:03Z")

</div>

Just wondering, why are you trying to do this?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 3, 2020, 2:04pm UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452/3 "2020-03-03T14:04:21Z")

</div>

This is from the I-really-hope-someone-improves-on-this school of problem solving, but

```julia
function solutionfunction(T::Type{<:Tuple})
    combinations = collect(Iterators.product(map(Base.uniontypes, fieldtypes(T))...))
    Union{map(c -> Tuple{c...}, combinations)...}
end

```

is a start:

```julia
julia> solutionfunction(Tuple{Union{Int, String}, Bool})
Union{Tuple{Int64,Bool}, Tuple{String,Bool}}

```

I am not sure if I am supposed to use `Base.uniontypes` or not.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 5, 2020, 6:48am UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452/4 "2020-03-05T06:48:22Z")

</div>

I am currently experimenting with type-inference

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [March 5, 2020, 6:53am UTC](https://discourse.julialang.org/t/transform-tuple-union-nonunion-union-to-union-tuple-nonunion-nonunion-nonunion/35452/5 "2020-03-05T06:53:18Z")

</div>

thanks a lot @Tamas_Papp

I just understood your solution. It is very concise. Thank you very much for your effort!
