# Extending Base.:show for Array of types

**URL:** <https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289>\
**Category:** General Usage\
**Created:** [November 19, 2019, 11:50pm UTC](https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289 "2019-11-19T23:50:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 19, 2019, 11:50pm UTC](https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289/1 "2019-11-19T23:50:04Z")

</div>

OK, I have this type

```julia
julia> D
3-element Array{GMT.GMTdataset,1}:
 GMT.GMTdataset([1.0 0.0; 2.0 -1.4100124638881568; 3.0 -0.757574139629664], String[], " -W,red,dashdot", String[], "", "")
 GMT.GMTdataset([1.0 0.0; 2.0 -1.2191288975583854; 3.0 0.47296208583982047], String[], " -W,green,dashdot", String[], "", "")
 GMT.GMTdataset([1.0 0.0; 2.0 -1.052594191811788; 3.0 0.9337415350728964], String[], " -W,blue,dashdot", String[], "", "")

```

and want to extend the show method to better display its contents, but if I define it like

```julia
function Base.:show(io::IO, D::Array{GMTdataset, 1})
	println("PASSED HERE")
end

```

then this method is not executed (get the same display as above). However, if I changed it to _scalar_

```julia
function Base.:show(io::IO, D::GMTdataset)
	println("PASSED HERE")
end

```

then I see this strange thing

```julia
julia> D
3-element Array{GMT.GMTdataset,1}:
PASSED HERE
PASSED HERE
PASSED HERE
 PASSED HERE
PASSED HERE

 PASSED HERE
PASSED HERE

 PASSED HERE
PASSED HERE

```

The method is called apparently 6 times. Three when it prints `PASSED HERE` and other three when it prints

```julia
 PASSED HERE
PASSED HERE

```

So, my question is how can capture an Array of types in show, and the heck of recursivity is happening here?

Thanks

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 20, 2019, 12:51am UTC](https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289/2 "2019-11-20T00:51:02Z")

</div>

The debugger is your friend here: if you know that `display` is what gets called from the REPL,

```julia
julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> show(a)
[1, 2, 3]
julia> display(a)
3-element Array{Int64,1}:
 1
 2
 3

```

then you can `@enter display(a)` and see how it works.

Short answer is that the 3-arg `show(io::IO, ::MIME"text/plain", X::AbstractArray)` is what you should consider specializing.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [November 20, 2019, 11:38am UTC](https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289/3 "2019-11-20T11:38:33Z")

</div>

Thanks a lot. This one would have been a difficult one to find for me, but got the message on how the debugger can be used for these cases.

---

<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:** [November 20, 2019, 4:20pm UTC](https://discourse.julialang.org/t/extending-base-show-for-array-of-types/31289/4 "2019-11-20T16:20:44Z")

</div>

> [@tim.holy](#):
>
> The debugger is your friend here

The [manual is hopefully even more helpful](https://docs.julialang.org/en/latest/manual/types/#man-custom-pretty-printing-1). Naive users of the debugger might be tempted to overload `display`, which is wrong. The manual explains that the 3-argument version of `show` is used for multi-line display in the REPL. (By default, it calls the 2-argument version of `show`, but complicated types tend to implement both.)
