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

question
#1

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?

#2
julia> Base.typename(typeof(x))
MyVec
#3

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?

#4

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