If I got my custom type MyType I’d like to keep the default printing when showing it isolated, but when in an array I’d like it to only show one of its fields
NWE:
struct MyType
foo
bar
end
m = MyType(1, 2)
Base.show(io, m::MyType) = print(io, m.foo) # will cause both cases to only show the field.
struct MyType
foo
bar
end
m = MyType(1, 2)
function Base.show(io, m::MyType)
if get(io, :compact, false)
print(io, m.foo)
else
Base.show_default(io, m)
end
end
Base.show(io::IO, ::MIME"text/plain", m::MyType) = Base.show_default(io, m)
julia> m
MyType(1, 2)
# okay
julia> [m]
1-element Vector{MyType}:
1-element Vector{MyType}:
Error showing value of type Vector{MyType}:
ERROR: MethodError: no method matching display(::Vector{MyType})
re-reading that part of the manual, I realized how much words we spent talking about things that are unlikely to be very relevant for someone looking this up
operator precedence and philosophy of repr (i.e. paste result back will evaluate correctly)
some “parsing” and metaprogramming constructs (:(...))
And then finally we talk about show(IOContext(stdout, :compact=>true), Polar(3, 4.0)). I think we should just cut straight to :compact => true. Or at the very least, change the order of these two paragraphs.
that’s very nice! thanks for taking the time to write that summary.
I guess I was mostly questioning the utility of a super specific example (complex number) and also mixing other concepts (repr roundtrip-ability, meta programming) unnecessarily
But even with the correction it doesn’t seem to use the compact path:
struct MyType
foo
bar
end
m = MyType(1, 2)
function Base.show(io::IO, m::MyType)
if get(io, :compact, false)
print(io, m.foo)
else
Base.show_default(io, m)
end
end
Base.show(io::IO, ::MIME"text/plain", m::MyType) = Base.show_default(io, m)
julia> m
MyType(1, 2)
julia> [m]
1-element Vector{MyType}:
MyType(1, 2)
Also, as commented here, the :compact=>true flag is only set when printing multiple columns. You could use get(io, :typeinfo, Any) === MyType instead, for example.
struct MyType
foo
bar
end
m = MyType(1, 2)
function Base.show(io::IO, ::MIME"text/plain", m::MyType)
if get(io, :typeinfo, Any) === MyType
print(io, m.foo)
else
Base.show_default(io, m)
end
end
julia> m
MyType(1, 2)
julia> [m]
1-element Vector{MyType}:
1