Skip to content

Docs on multi vs. single line display of custom types #36253

@baggepinnen

Description

@baggepinnen

After having read https://docs.julialang.org/en/latest/manual/types/#man-custom-pretty-printing-1 I was confused as to why displaying my type p::MyType and a vector of such object [p, p] both appeared to call the same three-arg method of show. It appears that if both three and two-arg methods

Base.show(io::IO, p::MyType)
Base.show(io::IO, ::MIME"text/plain", p::MyType)

are defined, a check is performed to see whether or not

Base.show(io::IO, ::MIME"text/plain", p::MyType)

actually prints on several lines or not, and the

Base.show(io::IO, p::MyType)

is only called if the printed string spans multiple lines. This caused me quite a bit of confusion and I wonder if this is the intended behavior and needs better documentation, or if it's a bug.

Below is an example that demonstrates what's confusing.

julia> struct MyType
           a
       end

julia> function Base.show(io::IO, p::MyType)
           print(io, "2-arg: ", p.a)
       end

julia> function Base.show(io::IO, ::MIME"text/plain", p::MyType) 
           print(io, "3-arg: ", p.a)
       end

julia> p = MyType(1)
3-arg: 1

julia> [p,p] # This still calles 3-arg, which confused me
2-element Array{MyType,1}:
 3-arg: 1
 3-arg: 1

julia> function Base.show(io::IO, ::MIME"text/plain", p::MyType) 
           print(io, "3-arg with newline:\n", p.a)
       end

julia> p = MyType(1)
3-arg with newline:
1

julia> [p,p] # Now the 2-arg method is called
2-element Array{MyType,1}:
 2-arg: 1
 2-arg: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    display and printingAesthetics and correctness of printed representations of objects.docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions