Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display default printing of composite types when it has a custom `show` defined

Tags:

julia

If a package author has defined a custom show function for their composite type, is there a way to easily print the default show? That is, what Julia would have shown for the composite type before the customization?

I am using Juno to step through the code of complex functions to try and I want to see the data representation but the full structure of the struct isn't shown due to the custom printing.

like image 619
Alec Avatar asked Jan 12 '20 17:01

Alec


2 Answers

You can use Base.show_default.

For example, Measurements.jl defines custom printing of the Measurement type:

julia> using Measurements

julia> x = 3 ± 0.1
3.0 ± 0.1

julia> Base.show_default(stdout, x)
Measurement{Float64}(3.0, 0.1, 0x0000000000000003, Measurements.Derivatives((3.0, 0.1, 0x0000000000000003) => 1.0))
like image 135
giordano Avatar answered Nov 19 '22 09:11

giordano


You can use invoke to make sure the default show method is called:

julia> struct Bar
       a
       b
       c
       end

julia> Base.show(io::IO, b::Bar) = print(io, "Bar")

julia> Bar(1,2,3)
Bar

julia> invoke(show, Tuple{IO, Any}, stdout, Bar(1,2,3))
Bar(1, 2, 3)

Also note that dump can be very useful in that exact scenario:

julia> dump(Bar(1,2,3))
Bar
  a: Int64 1
  b: Int64 2
  c: Int64 3
like image 23
pfitzseb Avatar answered Nov 19 '22 08:11

pfitzseb