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

I have a UnionAll type MyVec:

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> typeof(x).name.wrapper
MyVec

julia> typeof(ans)
UnionAll

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

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

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> y{:z, Vector}([1,2,3])
MyVec{:z,Array{T,1} where T}([1, 2, 3])

Am I clear now?

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

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

julia> ans === Array
true
1 Like

I forgot there is a simple solution: pattern matching.

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

julia> getparamtype1(x)
:x

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