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

Display of array elements seems to use ::Mime:text/plain in juia1.5.0-beta1 #36072

Closed
jmichel7 opened this issue May 29, 2020 · 5 comments · Fixed by #36076
Closed

Display of array elements seems to use ::Mime:text/plain in juia1.5.0-beta1 #36072

jmichel7 opened this issue May 29, 2020 · 5 comments · Fixed by #36076
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@jmichel7
Copy link

I am not sure this is a bug, but at least it is an incompatible change which demands some explanation on how to proceed to get the same effect as before.

It is convenient to use a compact display when displaying array elements which does not reflect the type (which is no problem since the eltype is displayed at the beginning of the array display). On the other hand when the element is displayed alone the type can be written first to show it.

Here is an example of what I mean for a type "integers mod p" that I define:

   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.4.2 (2020-05-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> struct Mod{p} <: Number
          val::Int
          function Mod{p}(a::Integer) where {p}
                  new(mod(a,p))
          end
       end

julia> Base.show(io::IO,x::Mod{p}) where p=print(io,x.val)

julia> function Base.show(io::IO, ::MIME"text/plain", x::Mod{p}) where p
         print(io,typeof(x),": ")
         show(io,x)
       end

julia> Mod{19}(2)
Mod{19}: 2

julia> m=Mod{19}.([1 2 3;4 5 6])
2×3 Array{Mod{19},2}:
 1  2  3
 4  5  6

In julia1.5.0-beta1 the display of the array has changed, it seems to use ::MIME"text/plain"

julia> m=Mod{19}.([1 2 3;4 5 6])
2×3 Array{Mod{19},2}:
 Mod{19}: 1  Mod{19}: 2  Mod{19}: 3
 Mod{19}: 4  Mod{19}: 5  Mod{19}: 6

What is the recommended way to get the same effect as before?

@fredrikekre fredrikekre added the display and printing Aesthetics and correctness of printed representations of objects. label May 29, 2020
@kimikage
Copy link
Contributor

cf. #34734

@JeffBezanson
Copy link
Member

Check the :compact flag in the MIME show method.

@jmichel7
Copy link
Author

jmichel7 commented May 29, 2020

OK. If I understand

Base.show(io::IO,x::Mod{p}) where p=print(io,x.val)

function Base.show(io::IO, ::MIME"text/plain", x::Mod{p}) where p
    print(io,typeof(x),": ")
    show(io,x)      
end

should be replaced by

function Base.show(io::IO, ::MIME"text/plain", x::Mod{p}) where p
    if !get(io,:compact,false) print(io,typeof(x),": ") end
    print(io,x.val)
end

I hope the documentation and/or the release notes for 1.5 make this clear. I could not get it from
the doctsring for show

JeffBezanson added a commit that referenced this issue May 29, 2020
JeffBezanson added a commit that referenced this issue May 29, 2020
JeffBezanson added a commit that referenced this issue Jun 1, 2020
@jmichel7
Copy link
Author

jmichel7 commented Jun 4, 2020

I feel the issue is not closed, because checking :compact still gives a different behaviour for vectors compared to 1.4.2:

julia> [Mod{19}(1), Mod{19}(2)] # in 1.5.0-beta1
2-element Array{Mod{19},1}:
 Mod{19}: 1
 Mod{19}: 2
julia> [Mod{19}(1), Mod{19}(2)] # in 1.4.2
2-element Array{Mod{19},1}:
 1
 2

I want to show the type only if it has not been shown already. Actually, exploring the io.dict I found that the following does the job:

julia> function Base.show(io::IO, ::MIME"text/plain", x::Mod{p}) where p
  if !haskey(io,:typeinfo) print(io,typeof(x),": ") end
  print(io,x.val)
end

so this leaves me confused, because the role of :compact seems to make a distinction between 1-dimensional and higher-dimensional arrays rather than telling the type has been displayed.

@JeffBezanson
Copy link
Member

That's because of #22981 --- :compact is not set for 1-column arrays. I'm not a big fan of that change but the ship has sailed.

In any case, :typeinfo is the correct answer for this type --- that is the correct flag to use regardless of whether arrays set :compact.

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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants