I am trying to define a custom type with convenient methods for readout, and having some very basic trouble. I have read the documentation for show with no luck in sorting this out. Defining a method for Base.show(::IO, ::MyType) is simple enough, but when MyType is part of an array, the printout is repeated several times per instance in the array:
struct MyType
x
end
function Base.show(io::IO, m::MyType)
println("instance of MyType:")
println("x = ", m.x)
end
m = MyType(10)
M = [m, m]
At the console:
julia> m
instance of MyType:
x = 10
julia> M
2-element Array{MyType,1}:
instance of MyType:
x = 10
instance of MyType:
x = 10
instance of MyType:
x = 10
instance of MyType:
x = 10
instance of MyType:
x = 10
instance of MyType:
x = 10
If anyone knows the reason for this strange output, any help would be much appreciated!
One suspicion of mine is that this is somehow the result of showcompat
, about which the documentation says: “a custom type should test get(io, :compact, false) in its normal show method”. However, I have not been able to find any examples of this in practice, and am not entirely sure what is meant by “test”.
Regardless of whether it is relevant to the problem I am experiencing, I would also like to define and use a showcompact
method for some custom types to print nicely in arrays, and so far have not been able to find an example of that anywhere, nor another discussion topic mentioning this.
So if anyone knows the answer to these, or can point me in the right direction, I would be much obliged!