Can I disable overloaded Base.show() and print the raw form of an object?

Sometimes I’d like to see the “raw” form of a custom struct. Can I print it using the default Base.show(), ignoring my own custom definitions which print prettier results but may hide details useful for debugging?

I think you want the dump function.

Sometimes dump works, but when my struct contains nontrivial data structures like Dict, I’ll get lengthy unreadable results, because literally all the subfields are dumped. I just want to temporarily disable my overloaded version of Base.show, but maybe there’s just no good way to do that.

You can use the maxdepth keyword argument to control how deeply dump prints:

struct A{S, T}
    x::S
    y::T
end

a = A(1:4, Dict(:a => 1, :b => 2));
julia> dump(a; maxdepth=1)
A{UnitRange{Int64}, Dict{Symbol, Int64}}
  x: UnitRange{Int64}
  y: Dict{Symbol, Int64}
1 Like

You can use invoke(show, Tuple{IO,Any}, stdout, x) to invoke the default show(::IO, ::Any) method instead of any overloaded definition for x:

julia> invoke(show, Tuple{IO,Any}, stdout, pi)
Irrational{:π}()

Alternatively, you can call Base.show_default(io, x) (which happens to be what show(::IO, ::Any) calls), but invoke is the more general technique to call a less-specific method.

7 Likes