How to print array contents in v0.7 (Base.showarray in v0.7)


#1

In v0.6 there is showarray, but it seems that it is changed in v0.7, how should I print some self-made type that implemented Array interface in v0.7?


#2
julia> struct A{T, N} <: AbstractArray{T, N} end

julia> Base.show(io::IO, ::MIME"text/plain", v::A) = print(io, "hello")

julia> A{Float64, 2}()
hello

#3

And for more details: https://docs.julialang.org/en/latest/manual/types/#man-custom-pretty-printing-1


#4

Thank you, but I mean in v0.6 if I implement getindex and size. I don’t need to rewrite a show function to print the array and I can use showarray to print the array without header.

struct MyArray{T, N} <: AbstractArray{T, N}
    data::Array{T, N}
end

import Base: getindex, size

size(x::MyArray) = size(x.data)
getindex(x::MyArray, index...) = getindex(x.data, index...)

I just don’t want to rewrite how to print an multi-dimensional array…


#5

What’s the problem on 0.7? I get this with your code:

julia> MyArray([1])
1-element MyArray{Int64,1}:
 1

julia> MyArray([1 2])
1Ă—2 MyArray{Int64,2}:
 1  2

#6

Yeah, but I don’t want the header, there was a keyword for Base.showarray to print only the content of the array without the header…


#7

OK, so you didn’t tell us the whole story. Base.showarray was an internal function which isn’t supposed to be used from outside Base. You can have a look at Base.print_array on 0.7, but its name could change again in the future.


#8

Ah… I see, thanks! But is it possible to print without header using exported functions?


#9

I don’t think so. You could just remove the summary line manually. Maybe there should be an IOContext option to omit it.


#10

This is what I went with in Rotations.jl:

(the !isa(X, RotMatrix) case is probably not interesting to you, but the rest is. Note that this still uses undocumented functionality from Base, just different undocumented functionality.


#11

Sorry, I didn’t get what will the keyword :compat do here? And how do you keep your v0.6 version compatible? I notice that summary do not have summary(io, x) but only have summary(x) in v0.6? And it seems that Compat does not contain this function…


#12

It’s :compact, not :compat. The 0.6 version is above this (this is in the else clause of a @static if VERSION < v"0.7-" statement).

I notice that summary do not have summary(io, x)

Yep, that looks like a mistake in the old code.


#13

Thanks, this is quite helpful!


#14

You can just use print(array) if you want very compact output, or witedlm if you want some delimited format.


#15

Thanks, it is similar to showarray indeed. I’m just not familiar with IO part, will it print the header, if the io is re-direct to something similar to REPL? I would like my custom array have a custom header but with similar behavior with built-in Array, so my user won’t learn much.


#16

Have you tried to simply define a custom summary method?


#17

Oh, I see, I tried this, but it works fine in v0.6 but it won’t print my custom header in v0.7:

struct MyArray{T, N} <: AbstractArray{T, N}
    data::Array{T, N}
end

import Base: size, getindex

size(x::MyArray) = size(x.data)
getindex(x::MyArray, index...) = getindex(x.data, index...)

import Base: show, summary

show(io::IO, x::MyArray) = show(io, x.data)

@static if VERSION < v"0.7-"
    summary(x::MyArray) = "hello"
else
    summary(io::IO, x::MyArray) = "hello"
end

in v0.6:

julia> MyArray(rand(2, 2))
hello:
 0.296337  0.719684
 0.231015  0.764015

v0.7

julia> MyArray(rand(2, 2))
:
 0.18533   0.65715 
 0.376326  0.629767

#18
summary(io::IO, x::MyArray) = "hello"

should be

summary(io::IO, x::MyArray) = print(io, "hello")
julia> MyArray(rand(2, 2))
hello:
 0.527634  0.101987
 0.452496  0.404595