Hi all,
I would like to know if it is possible to customize the output of the typeof
function for a user defined struct
? I have rather long type names (recursive types) and I would like to simplify the error messages.
Thank you for your help.
1 Like
This is a straightforward application of custom pretty printing to the Type{...}
singleton:
julia> struct Foo; end
julia> Base.show(io::IO, ::Type{Foo}) = print(io, "this is a Foo type")
julia> x = Foo()
this is a Foo type()
julia> typeof(x)
this is a Foo type
You can use T::Type{<:Foo}
to handle a whole set of types.
(Beware that changing show
for types should not throw, however, and in general is a finicky low-level thing. In that thread, @JeffBezanzon says I never condone defining show for particular types, which goes beyond “type piracy” and into “type treason”)
7 Likes
Thanks a lot ! I have tout pickup my english dictionnary to make sure I have understood the warning.
1 Like