Extending Base.:show for Array of types

OK, I have this type

julia> D
3-element Array{GMT.GMTdataset,1}:
 GMT.GMTdataset([1.0 0.0; 2.0 -1.4100124638881568; 3.0 -0.757574139629664], String[], " -W,red,dashdot", String[], "", "")
 GMT.GMTdataset([1.0 0.0; 2.0 -1.2191288975583854; 3.0 0.47296208583982047], String[], " -W,green,dashdot", String[], "", "")
 GMT.GMTdataset([1.0 0.0; 2.0 -1.052594191811788; 3.0 0.9337415350728964], String[], " -W,blue,dashdot", String[], "", "")

and want to extend the show method to better display its contents, but if I define it like

function Base.:show(io::IO, D::Array{GMTdataset, 1})
	println("PASSED HERE")
end

then this method is not executed (get the same display as above). However, if I changed it to scalar

function Base.:show(io::IO, D::GMTdataset)
	println("PASSED HERE")
end

then I see this strange thing

julia> D
3-element Array{GMT.GMTdataset,1}:
PASSED HERE
PASSED HERE
PASSED HERE
 PASSED HERE
PASSED HERE

 PASSED HERE
PASSED HERE

 PASSED HERE
PASSED HERE

The method is called apparently 6 times. Three when it prints PASSED HERE and other three when it prints

 PASSED HERE
PASSED HERE

So, my question is how can capture an Array of types in show, and the heck of recursivity is happening here?

Thanks

The debugger is your friend here: if you know that display is what gets called from the REPL,

julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> show(a)
[1, 2, 3]
julia> display(a)
3-element Array{Int64,1}:
 1
 2
 3

then you can @enter display(a) and see how it works.

Short answer is that the 3-arg show(io::IO, ::MIME"text/plain", X::AbstractArray) is what you should consider specializing.

5 Likes

Thanks a lot. This one would have been a difficult one to find for me, but got the message on how the debugger can be used for these cases.

The manual is hopefully even more helpful. Naive users of the debugger might be tempted to overload display, which is wrong. The manual explains that the 3-argument version of show is used for multi-line display in the REPL. (By default, it calls the 2-argument version of show, but complicated types tend to implement both.)

1 Like