Print name of type w/o parameters, Julia 1.0-master

Given a parametric type T I would like to print (or, equivalently, obtain as a String) the name of this type without parameters. The purpose is pretty-printing a complicated type.

I need a generic solution, not one that depend on defining methods for particular types. Base.typename used to work for this, but recent changes on master broke this.

MWE of how I did it previously:

struct Foo{N} end

struct Bar{T}
    x::T
end

Base.show(io::IO, b::Bar) = print(io, "Bar with $(Base.typename(typeof(b.x)))")

julia> Bar(Foo{Int}())
Bar with Foo

but note

julia> VERSION
v"1.6.0-DEV.707"

julia> Bar(Foo{Int}())
Bar with typename(Foo)

Ideally a solution would work for all versions, but I can also condition this code or use Compat.

1 Like

I am wondering if the recent changes in master are maybe unintentional. Given that it seems to be the only reason to change your code, reverting that might be the better solution?

I am asking mostly since it looks weird to me that typename() outputs “typename”. That looks redundant…

Well, since it is not exported, I can’t complain.

In the meantime, I realized that I can just use nameof and it does what I want.

3 Likes