Get simple name of type in Julia?

Viewed 161

Say I have

struct MyStruct{T,U}
  a::T
  b::U
end

I'd like to define a custom show which eliminates a lot of noise from the full type.

E.g. if I create the following:

z = MyStruct((a=1,b=2),rand(5))

then typeof shows much more than I want:

julia> typeof(z)
MyStruct{NamedTuple{(:a, :b), Tuple{Int64, Int64}}, Vector{Float64}}

How can I programmatically get just MyStruct from z into a string?

1 Answers

Some lengthy discussions on Discourse here and here, which provide at least these two solutions (the second one generalising the first):

julia> Base.typename(typeof(z)).wrapper
MyStruct

julia> name(::Type{T}) where {T} = (isempty(T.parameters) ? T : T.name.wrapper)
name (generic function with 1 method)

julia> name(typeof(z))
MyStruct
Related