# More elegant way to get the \`UnionAll\` type from an instance?

**URL:** https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439
**Category:** General Usage
**Tags:** question
**Created:** [March 28, 2019, 3:21am UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439 "2019-03-28T03:21:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [March 28, 2019, 3:21am UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439/1 "2019-03-28T03:21:05Z")

</div>

I have a `UnionAll` type `MyVec`:

```julia
julia> struct MyVec{a,A <: AbstractVector}
           data::A
           function MyVec{a,A}(data) where {a,A}
               new(data)
           end
       end

julia> x = MyVec{:x, Vector}(rand(3))
MyVec{:x,Array{T,1} where T}([0.079421, 0.49293, 0.0557392])

```

How can I get the `MyVec` type from `x`? Currently I am using

```julia
julia> typeof(x).name.wrapper
MyVec

julia> typeof(ans)
UnionAll

```

Is there a more elegant way to get the `MyVec` type?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 28, 2019, 5:13am UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439/2 "2019-03-28T05:13:40Z")

</div>

```julia
julia> Base.typename(typeof(x))
MyVec

```

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [March 29, 2019, 4:52am UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439/3 "2019-03-29T04:52:55Z")

</div>

I am sorry, I might not be clear in my question. I do not want a `Core.TypeName` `MyVec`, I want a type `MyVec`. For example, if `y` is what I want. Then I can do

```julia
julia> y{:z, Vector}([1,2,3])
MyVec{:z,Array{T,1} where T}([1, 2, 3])

```

Am I clear now?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 29, 2019, 5:18am UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439/4 "2019-03-29T05:18:34Z")

</div>

Not an elegant way, but if you want to use only public API, here is another way:

```julia
julia> let x = []
           T = typeof(x)
           getproperty(parentmodule(T), nameof(T))
       end
Array

julia> ans === Array
true

```

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 20, 2019, 6:24pm UTC](https://discourse.julialang.org/t/more-elegant-way-to-get-the-unionall-type-from-an-instance/22439/5 "2019-06-20T18:24:24Z")

</div>

I forgot there is a simple solution: pattern matching.

```julia
getunionalltype(::MyVec) = MyVec
getparamtype1(::MyVec{a}) where {a} = a
getparamtype2(::MyVec{a,A}) where {a,A} = A

```

```julia
julia> getunionalltype(x)
MyVec

julia> getparamtype1(x)
:x

julia> getparamtype2(x)
Array{T,1} where T

```
