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

**URL:** <https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303>\
**Category:** Internals & Design\
**Created:** [April 12, 2018, 1:24pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303 "2018-04-12T13:24:57Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 1:24pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/1 "2018-04-12T13:24:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 12, 2018, 1:28pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/2 "2018-04-12T13:28:17Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 12, 2018, 1:40pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/3 "2018-04-12T13:40:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 1:42pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/4 "2018-04-12T13:42:49Z")

</div>

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.

```julia
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…

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 12, 2018, 1:54pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/5 "2018-04-12T13:54:22Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 1:56pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/6 "2018-04-12T13:56:05Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 12, 2018, 2:02pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/7 "2018-04-12T14:02:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 2:03pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/8 "2018-04-12T14:03:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 12, 2018, 2:07pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/9 "2018-04-12T14:07:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 12, 2018, 2:11pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/10 "2018-04-12T14:11:51Z")

</div>

This is what I went with in Rotations.jl:

[https://github.com/FugroRoames/Rotations.jl/blob/a405b90e3856bd5025f7de6f959bcc1d4ec33b9e/src/core\_types.jl#L196-L217](https://github.com/FugroRoames/Rotations.jl/blob/a405b90e3856bd5025f7de6f959bcc1d4ec33b9e/src/core_types.jl#L196-L217)

(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.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 2:22pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/11 "2018-04-12T14:22:12Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 12, 2018, 2:37pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/12 "2018-04-12T14:37:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 12, 2018, 3:26pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/13 "2018-04-12T15:26:30Z")

</div>

Thanks, this is quite helpful!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2018, 2:30am UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/14 "2018-04-13T02:30:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 13, 2018, 5:58am UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/15 "2018-04-13T05:58:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 13, 2018, 6:51am UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/16 "2018-04-13T06:51:00Z")

</div>

Have you tried to simply define a custom `summary` method?

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [April 13, 2018, 6:58am UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/17 "2018-04-13T06:58:35Z")

</div>

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

```julia
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
julia> MyArray(rand(2, 2))
hello:
 0.296337 0.719684
 0.231015 0.764015

```

v0.7

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

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 13, 2018, 1:09pm UTC](https://discourse.julialang.org/t/how-to-print-array-contents-in-v0-7-base-showarray-in-v0-7/10303/18 "2018-04-13T13:09:21Z")

</div>

```julia
summary(io::IO, x::MyArray) = "hello"

```

should be

```julia
summary(io::IO, x::MyArray) = print(io, "hello")

```

```julia
julia> MyArray(rand(2, 2))
hello:
 0.527634 0.101987
 0.452496 0.404595

```
