# Retrieve member types of a Union

**URL:** <https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 9, 2017, 12:52am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335 "2017-10-09T00:52:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 9, 2017, 12:52am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335/1 "2017-10-09T00:52:02Z")

</div>

Is there a standard way to get the member types of a `Union`?

In v0.5 I used, but was never happy with:

```julia
typealias Primitive_UInt Union{UInt8,UInt16,UInt32,UInt64,UInt128}
Primitive_UInt.types

```

In v0.6 I’ve experimented, without fully understanding what I’m doing, to come up with:

```julia
members(x::DataType) = (x,)
members(x::Union) = (x.a, members(x.b)...)

const Primitive_UInt = Union{UInt8,UInt16,UInt32,UInt64,UInt128}
members(Primitive_UInt)

```

Is there a better or standard way?

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 9, 2017, 1:28am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335/2 "2017-10-09T01:28:03Z")

</div>

Is this what you are looking for?

```julia
julia> Base.uniontypes(Primitive_UInt)
5-element Array{Any,1}:
 UInt8
 UInt16
 UInt32
 UInt64
 UInt128

```

The definition of `uniontypes()` (in [base/reflection.jl](https://github.com/JuliaLang/julia/blob/19b3ca7be9f47099dcf192b57e3b044c571ed155/base/reflection.jl#L628)) is not too different from your `members()`:

```julia
_uniontypes(x::Union, ts) = (_uniontypes(x.a,ts); _uniontypes(x.b,ts); ts)
_uniontypes(x::ANY, ts) = (push!(ts, x); ts)
uniontypes(x::ANY) = _uniontypes(x, Any[])

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 9, 2017, 1:41am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335/3 "2017-10-09T01:41:43Z")

</div>

Thanks! This is exactly what I’m looking for.

Presumably not important enough to export, but I hope it’s reliably “official”.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [October 9, 2017, 2:31am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335/4 "2017-10-09T02:31:50Z")

</div>

Yeah, `Base.uniontypes` is fine, though if you’re concerned about performance/memory, then your original approach seems better IMO.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 9, 2017, 2:49am UTC](https://discourse.julialang.org/t/retrieve-member-types-of-a-union/6335/5 "2017-10-09T02:49:48Z")

</div>

Neither is inferrable and the approach in this post is `O(N^2)` instead of `O(N)`.
