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

Weird behaviour with show of custom types #45776

Open
jakobnissen opened this issue Jun 22, 2022 · 1 comment
Open

Weird behaviour with show of custom types #45776

jakobnissen opened this issue Jun 22, 2022 · 1 comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. docs This change adds or pertains to documentation

Comments

@jakobnissen
Copy link
Contributor

jakobnissen commented Jun 22, 2022

The docstring of IOContext says:

:compact: Boolean specifying that values should be printed more compactly, e.g. that numbers should be printed with fewer digits. This is set when printing array elements.

That appears to be wrong after #22981:

julia> struct Foo end

julia> function Base.show(io::IO, ::MIME"text/plain", ::Foo)
           if get(io, :compact, false)
               print(io, "compact")
           else
               print(io, "verbose")
           end
       end;

julia> [Foo()]
1-element Vector{Foo}:
 verbose

And here is something even wilder: Calling println instead of print in the definition of show causes it to dispatch to a different method. I have zero idea what is going on here.

julia> struct Foo end

julia> Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "compact")

julia> [Foo()]
1-element Vector{Foo}:
 compact

julia> Base.show(io::IO, ::MIME"text/plain", ::Foo) = println(io, "compact") # nb: println instead of print

julia> [Foo()]
1-element Vector{Foo}:
 Foo()

Beside these two issues which I think are bugs, it's completely unclear to me how to define a custom verbose display for a new type, and not use this display when inside a container. So the documentation here could probably be improved

@JeffBezanson
Copy link
Member

Yes we should change the documentation. We should say that it MIGHT be set when printing array elements, e.g. if the container type in question thinks it has little space per element. But that's entirely up to the container.

The println thing is happening because arrays attempt printing elements more verbosely, but if the output contains a line break it calls a different function instead since the line break would mess up the array display. I don't think you can reasonably expect to control how your type is printed inside a certain type of container, since again that is up to the container.

@JeffBezanson JeffBezanson added docs This change adds or pertains to documentation display and printing Aesthetics and correctness of printed representations of objects. labels Jun 22, 2022
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

2 participants