Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baggepinnen opened this issue Jun 12, 2020 · 4 comments
Open

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

baggepinnen opened this issue Jun 12, 2020 · 4 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects. docs This change adds or pertains to documentation

Comments

@baggepinnen
Copy link
Contributor

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
@mcabbott
Copy link
Contributor

I was confused by this too, updating some code. This doesn't seem to happen on Julia 1.3 and earlier, i.e. [p,p] prints the 2-arg form on 1.1 - 1.3.

@tkf
Copy link
Member

tkf commented Jun 13, 2020

@thofma
Copy link
Contributor

thofma commented Jan 2, 2022

A dup of #36072? #36076 already clarifies this a lot in the docstrings:

https://docs.julialang.org/en/v1.6-dev/base/io-network/#Base.show-Tuple{IO,Any,Any} https://docs.julialang.org/en/v1.6-dev/base/io-network/#Base.show-Tuple{IO,Any}

I could not find where it is documented, that the 2-arg version is used if the 3-arg version prints over multiple lines. Which docstring contains this?

@brenhinkeller brenhinkeller added docs This change adds or pertains to documentation display and printing Aesthetics and correctness of printed representations of objects. labels Nov 20, 2022
@exaexa
Copy link
Contributor

exaexa commented Jan 3, 2023

Adding myself to the cc: list of confused people here.

EDIT: #37072 and #36076 didn't help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

6 participants