How to print name of parametric type without, uhm, parameters?

For example:

julia> struct A{T}
       a::T
       end

julia> simpletypename1(x::F) where F = string(F);

julia> @assert simpletypename1(A(3)) === "A" # Fails, as type is A{Int64} 

julia> simpletypename2(x::F) where F = string(F.name); # Yeah I know, not a good idea...

julia> @assert simpletypename2(A(3)) === "A" # Works in 1.5 but fails in 1.6; prints "typename(A)"

julia> simpletypename3(x::F) where F = string(F.name.name); # Ugh...

julia> @assert simpletypename3(A(3)) === "A" # Phyrric victory...

Regex/split is ofc an option but I’m thinking there is a better way.

1 Like

That?

julia> struct A{T}
         x::T
       end

julia> a = A(1)
A{Int64}(1)

julia> typeof(a).name
A
2 Likes

Prints “typename(A)” in 1.6 :frowning:

All three of these work in both 1.5 and 1.6

julia> Vector{Set{Int}}.name.name
:Array

julia> Base.typename(Vector{Set{Int}}).name
:Array

julia> nameof(Vector{Set{Int}})
:Array
2 Likes

Julia version 1.6:

julia> struct A{T}
                     a::T
                     end

julia> nameof( typeof( A(3) ) )
:A

julia> string( nameof( typeof( A(3) ) ) )
"A"

julia> simpletypename4(x) = string( nameof( typeof( x ) ) )
simpletypename4 (generic function with 1 method)

julia> simpletypename4( A(3) )
"A"

julia> versioninfo()
Julia Version 1.6.0-beta1.0
Commit b84990e1ac (2021-01-08 12:42 UTC)
...

Julia version 1.5:

julia> struct A{T}
              a::T
              end

julia> nameof( typeof( A(3) ) )
:A

julia> string( nameof( typeof( A(3) ) ) )
"A"

julia> simpletypename4(x) = string( nameof( typeof( x ) ) )
simpletypename4 (generic function with 1 method)

julia> simpletypename4( A(3) )
"A"

julia> versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
3 Likes

Awesome thanks alot. Can only select one solution so I picked the first.