# Way to check if variable is of type Union?

**URL:** <https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 22, 2018, 6:34pm UTC](https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351 "2018-09-22T18:34:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [September 22, 2018, 6:34pm UTC](https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351/1 "2018-09-22T18:34:51Z")

</div>

Pretty abstract question that I can’t really wrap my head around.

What is a robust way of checking if something is a Union?

* * *

For example, (from the [docs](https://docs.julialang.org/en/v1/manual/types/index.html#Operations-on-Types-1))

```julia
> typeof(Union{Real,String})
Union

```

```julia
> typeof(Union{AbstractFloat})
DataType

```

* * *

The use case for this is calling `subtype` and `supertype`

So first question, can you just flat-out find out if something is a `Union`?

And a follow-up, is there a way to broadcast `sub`/`super`-`type` over all the params that appear within a Union?

(i.e. `Real` and `String` for the first union above, `AbstractFloat` for the second)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 22, 2018, 6:39pm UTC](https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351/2 "2018-09-22T18:39:49Z")

</div>

Values always have concrete types (by definition)—an object cannot have an abstract type (and in particular it cannot have a union type). A variable could be inferred to have a union type, but that’s just an implementation detail of the optimizer. Locations like fields in structures or slots in arrays can be abstractly typed, and that can be queried with `fieldtype` and `eltype` like so:

```julia
julia> fieldtype(typeof(1//2), :num)
Int64

julia> eltype(rand(5))
Float64

```

---

<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:** [September 23, 2018, 12:34am UTC](https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351/3 "2018-09-23T00:34:12Z")

</div>

> [@djsegal](#):
>
> So first question, can you just flat-out find out if something is a `Union` ?

`typeof`. The case above is simply because `Union{}` of a single type is always that type itself.

> [@djsegal](#):
>
> (i.e. `Real` and `String` for the first union above, `AbstractFloat` for the second)

Well, the second one isn’t a union.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 23, 2018, 1:16am UTC](https://discourse.julialang.org/t/way-to-check-if-variable-is-of-type-union/15351/4 "2018-09-23T01:16:56Z")

</div>

```julia
tx = Union{Int64, Float64};
fx(::Int64) = print("Int");
fx(::Float64) = print("Float");
tarr = Array{tx}(undef, 10) ## 
tarr[1:2:end] .= 1.0;
tarr[2:2:end] .= 1; #  
fx.(tarr)
FloatIntFloatIntFloatIntFloatIntFloatInt

```
