# Destructuring a Union{...}

**URL:** <https://discourse.julialang.org/t/destructuring-a-union/13526>\
**Category:** General Usage\
**Created:** [August 15, 2018, 9:05pm UTC](https://discourse.julialang.org/t/destructuring-a-union/13526 "2018-08-15T21:05:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamii/32/1373_2.png) [@jamii](https://discourse.julialang.org/u/jamii)\
**Post date:** [August 15, 2018, 9:05pm UTC](https://discourse.julialang.org/t/destructuring-a-union/13526/1 "2018-08-15T21:05:03Z")

</div>

What is the idiomatic way to take apart a Union type? The obvious structural matching doesn’t do what I expected:

```julia
julia> f1(::Type{Union{X, Int64}}) where {X} = X
f1 (generic function with 1 method)

julia> f1(Union{Int64, Float64})
Float64

julia> f2(::Type{Union{X, Float64}}) where {X} = X
f2 (generic function with 1 method)

julia> f2(Union{Int64, Float64})
Int64

julia> f3(::Type{Union{X, Y}}) where {X, Y} = (X, Y)
f3 (generic function with 1 method)

julia> f3(Union{Int64, Float64})
ERROR: UndefVarError: Y not defined
Stacktrace:
 [1] f3(::Type{Union{Float64, Int64}}) at ./REPL[7]:1
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [August 15, 2018, 9:53pm UTC](https://discourse.julialang.org/t/destructuring-a-union/13526/2 "2018-08-15T21:53:59Z")

</div>

Huh, what ends up happening is that `X` matches `Union{Float64, Int64}` and Y is left undefined. I wonder if this is a bug.

I also wonder if there’s a way to use something like `where X >: Union{}` to constrain both X and Y to match something (this doesn’t work because `Union{} >: Union{}`).

---

<div class="post-metadata">

**Author:** ![jamii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamii/32/1373_2.png) [@jamii](https://discourse.julialang.org/u/jamii)\
**Post date:** [August 15, 2018, 10:33pm UTC](https://discourse.julialang.org/t/destructuring-a-union/13526/3 "2018-08-15T22:33:36Z")

</div>

Not a bug ([https://github.com/JuliaLang/julia/issues/28674#issuecomment-413351095](https://github.com/JuliaLang/julia/issues/28674#issuecomment-413351095)), but it certainly means that my mental model of unification is wrong. I guess matching is greedy?
