# Recommended way to introspect a Union type?

**URL:** <https://discourse.julialang.org/t/recommended-way-to-introspect-a-union-type/126380>\
**Category:** General Usage\
**Created:** [February 27, 2025, 11:03am UTC](https://discourse.julialang.org/t/recommended-way-to-introspect-a-union-type/126380 "2025-02-27T11:03:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 27, 2025, 11:03am UTC](https://discourse.julialang.org/t/recommended-way-to-introspect-a-union-type/126380/1 "2025-02-27T11:03:16Z")

</div>

I want to introspect a union type. More concretely I want  
a function that takes a type `T` of the form `T = Union{X,Y}`  
and returns `Set((X,Y))`. I can do this like `X = T.a` and `Y = T.b`.  
But that feels like internals. Is there an official way to do this?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 1, 2025, 4:06pm UTC](https://discourse.julialang.org/t/recommended-way-to-introspect-a-union-type/126380/2 "2025-03-01T16:06:57Z")

</div>

I think such manipulations are officially discouraged, with `T.a` and `T.b` being the only way to access `Union` parts.

---

<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:** [March 1, 2025, 4:23pm UTC](https://discourse.julialang.org/t/recommended-way-to-introspect-a-union-type/126380/3 "2025-03-01T16:23:20Z")

</div>

Feature request on Julia’s issue tracker on Github:

> <https://github.com/JuliaLang/julia/issues/53193>
>
> Useful functionality like \`Base.typesplit\` or \`Base.promote\_union\` may be implem…ented by recursively mapping over the components of a \`Union\` object. I wonder if it would make sense to provide a public API so users could do that themselves. I guess either of these would be nice:
> 
> 1. document and support the \`a\` and \`b\` fields of a \`Union\`, or
> 
> 2. provide some higher level API similar to \`mapreduce\`, that would take a type instead of a collection, interpreting union components as collection elements
