# Decompose Tuple or Union type into constituents?

**URL:** https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696
**Category:** General Usage
**Created:** [July 24, 2022, 4:56am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696 "2022-07-24T04:56:21Z")
**Posts on this page:** 12
**Page:** 1

<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: [July 24, 2022, 4:56am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/1 "2022-07-24T04:56:21Z")

</div>

Is it possible to iterate over the parameters of a `Tuple` or `Union`? Given `Tuple{A, B, C, D, E, ...}`, is it possible to extract the parameters (`A`, `B`, `C`, `D`, `E`, …)?

A partial solution/hack is something like this, for extracting only a single parameter by position:

```julia
# Extracts second Tuple parameter in a Tuple type with two elements.
f(::Type{Tuple{S, T}}) where {S <: Any, T <: Any} = T

# Extracts second Tuple parameter.
g(::Type{<:Tuple{<:Any, T}}) where {T <: Any} = T

```

A related question is: how do I convert an arbitrary tuple type to to a corresponding union, for example `Tuple{A, B, C, D}` would be converted to `Union{A, B, C, D}`. Or Perhaps even something like `Tuple{A, B, Tuple{C, D}}` to the same Union.

I don’t have a real use for this, just being curious.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 24, 2022, 5:05am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/2 "2022-07-24T05:05:16Z")

</div>

> [@nsajko](#):
>
> I don’t have a real use for this, just being curious.

```julia-auto
julia> a = (1, 1.2, "a")
(1, 1.2, "a")

julia> typeof(a).types
svec(Int64, Float64, String)

```

---

<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: [July 24, 2022, 5:06am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/3 "2022-07-24T05:06:40Z")

</div>

> [@jling](#):
>
> svec(Int64, Float64, String)

Interesting, never encountered a SimpleVector before!

---

<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: [July 24, 2022, 5:20am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/4 "2022-07-24T05:20:53Z")

</div>

They are not something users generally need to use. They are used by the compiler because it’s very useful to have a very simple list type that doesn’t have strict types.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 24, 2022, 6:09am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/5 "2022-07-24T06:09:11Z")

</div>

reiterating what @Oscar_Smith said …  
There really is no good reason for julia developers to ever do anything with `svecs` other than convert them to a tuple or, if appropriate, a vector. The `svec` is not a completed data structure – it does what Base requires of it, and does not do whatever else it might. Their use is a fast track to unexpected occurrences.

Another approach to the proposed function:

```julia
function eltypesof(x::T) where {T<:Union{Tuple, NamedTuple}}
   Tuple( typeof(x).types )
end

function eltypesof(x::Type{T}) where {T<:Union{Tuple, NamedTuple}}
    Tuple(x.types)
end

function eltypesof(x::Type{T}) where {T<:NamedTuple}
    Tuple(x.types) 
end

function eltypesof(x::U) where {U<:Union}
   Tuple( Base.uniontypes(x) )
end

eltypesof(::Type{Union{}}) = ()
# or, depending on the intended semantics
# eltypesof(::Type{Union{}}) = Core.TypeOfBottom

```

It also handles NamedTuples.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 24, 2022, 6:25am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/6 "2022-07-24T06:25:26Z")

</div>

> [@jling](#):
>
> ```julia
> julia> typeof(a).types
> svec(Int64, Float64, String)
> 
> ```

Why mess about with internal fields and `svec`s instead of just doing

```julia
julia> typeof.(a)
(Int64, Float64, String) 

```

?

---

<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: [July 24, 2022, 6:30am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/7 "2022-07-24T06:30:47Z")

</div>

The question as posed is for when only a Tuple type is available as input, not a tuple instance.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 24, 2022, 6:32am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/8 "2022-07-24T06:32:25Z")

</div>

now do Union

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [July 24, 2022, 6:39am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/9 "2022-07-24T06:39:37Z")

</div>

Okay, but the accepted answer was on a tuple instance, which is a bit confusing, then.

Or in other words, then assume my answer was a response to this particular line of code, instead of the OP:

> [@jling](#):
>
> ```julia
> julia> typeof(a).types
> svec(Int64, Float64, String)
> 
> ```

Why do this on a tuple?

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 24, 2022, 6:41am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/10 "2022-07-24T06:41:03Z")

</div>

thank you – see the revision above

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 24, 2022, 6:44am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/11 "2022-07-24T06:44:04Z")

</div>

you can use that as `Tuple(unique(typeof.(a)))`as long as there is no need to go into sub tuples, which is simply unclear in the question.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 24, 2022, 7:06am UTC](https://discourse.julialang.org/t/decompose-tuple-or-union-type-into-constituents/84696/12 "2022-07-24T07:06:29Z")

</div>

I have tested the revised routines for empty and nonempty Tuples and NamedTuples and Unions. Let me know if you find a state I have overlooked.
