# Print things differently in an array

**URL:** <https://discourse.julialang.org/t/print-things-differently-in-an-array/114563>\
**Category:** General Usage\
**Created:** [May 22, 2024, 1:07pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563 "2024-05-22T13:07:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [May 22, 2024, 1:07pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/1 "2024-05-22T13:07:47Z")

</div>

If I got my custom type `MyType` I’d like to keep the default printing when showing it isolated, but when in an array I’d like it to only show one of its fields

NWE:

```julia
struct MyType
   foo
   bar
end

m = MyType(1, 2)
Base.show(io, m::MyType) = print(io, m.foo) # will cause both cases to only show the field.

```

---

<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:** [May 22, 2024, 1:18pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/2 "2024-05-22T13:18:51Z")

</div>

> [@BeastyBlacksmith](#):
>
> If I got my custom type `MyType` I’d like to keep the default printing when showing it isolated, but when in an array I’d like it to only show one of its fields

You can use the `compact` flag of the [`IOContext`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext), which is set to `true` when being printed as part of a container. That is, call

```julia
if get(io, :compact, false)
    # show inside array
else
    # normal show
end

```

And/or you can define a [3-argument `show(io::IO, ::MIME"text/plain", m::MyType)` method](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) for verbose multiline display by the REPL.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [May 22, 2024, 1:29pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/3 "2024-05-22T13:29:26Z")

</div>

I’m doing something wrong

```julia
struct MyType
   foo
   bar
end

m = MyType(1, 2)
function Base.show(io, m::MyType)
         if get(io, :compact, false)
           print(io, m.foo)
         else
           Base.show_default(io, m)
         end
end

Base.show(io::IO, ::MIME"text/plain", m::MyType) = Base.show_default(io, m)

julia> m
MyType(1, 2)
# okay
julia> [m]
1-element Vector{MyType}:
1-element Vector{MyType}:
Error showing value of type Vector{MyType}:
ERROR: MethodError: no method matching display(::Vector{MyType})
```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 22, 2024, 1:37pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/4 "2024-05-22T13:37:28Z")

</div>

re-reading that part of the manual, I realized how much words we spent talking about things that are unlikely to be very relevant for someone looking this up

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f967b1831c8be32e8bb92129c43f70c36175d5b.jpeg)

For some reason we delve into the details of:

- operator precedence and philosophy of `repr` (i.e. paste result back will evaluate correctly)
- some “parsing” and metaprogramming constructs (`:(...)`)

And then finally we talk about `show(IOContext(stdout, :compact=>true), Polar(3, 4.0))`. I think we should just cut straight to `:compact => true`. Or at the very least, change the order of these two paragraphs.

---

<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:** [May 22, 2024, 1:55pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/5 "2024-05-22T13:55:59Z")

</div>

> [@jling](#):
>
> re-reading that part of the manual, I realized how much words we spent talking about things that are unlikely to be very relevant for someone looking this up

It’s the difference between a tutorial and a reference, I think, but perhaps the example could be simplified. Will hopefully be improved by:

> <https://github.com/JuliaLang/julia/pull/54547>
>
> This adds a brief summary of the different output functions (\`display\`, \`show\`, …\`print\`, \`repr\`, etc.) and how they interact to the custom pretty-printing section of the manual, following \[this post on discourse\](https://discourse.julialang.org/t/how-to-display-a-sparse-array-on-the-repl/113565/5), since people often get confused by this.

---

<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:** [May 22, 2024, 1:59pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/6 "2024-05-22T13:59:06Z")

</div>

> [@BeastyBlacksmith](#):
>
> I’m doing something wrong

You get more informative output from:

```julia
julia> show(stdout, "text/plain", [m])
1-element Vector{MyType}:
ERROR: MethodError: show(::IOContext{IOBuffer}, ::MyType) is ambiguous.

```

In particular, you should have defined the `show` method as:

```julia
function Base.show(io::IO, m::MyType)

```

(you were missing the type qualifier for `io`).

Would be good to improve the error thrown by `display` here: [confusing error thrown by `display` · Issue #54551 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/54551)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 22, 2024, 3:24pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/7 "2024-05-22T15:24:19Z")

</div>

that’s very nice! thanks for taking the time to write that summary.

I guess I was mostly questioning the utility of a super specific example (complex number) and also mixing other concepts (repr roundtrip-ability, meta programming) unnecessarily

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [May 23, 2024, 4:09pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/8 "2024-05-23T16:09:53Z")

</div>

But even with the correction it doesn’t seem to use the compact path:

```julia
struct MyType
   foo
   bar
end

m = MyType(1, 2)

function Base.show(io::IO, m::MyType)
         if get(io, :compact, false)
           print(io, m.foo)
         else
           Base.show_default(io, m)
         end
end

Base.show(io::IO, ::MIME"text/plain", m::MyType) = Base.show_default(io, m)

julia> m
MyType(1, 2)

julia> [m]
1-element Vector{MyType}:
 MyType(1, 2)

```

---

<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:** [May 23, 2024, 6:10pm UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/9 "2024-05-23T18:10:42Z")

</div>

> [@BeastyBlacksmith](#):
>
> But even with the correction it doesn’t seem to use the compact path:

Printing array elements apparently [uses 3-argument `show` unless it contains line breaks](https://github.com/JuliaLang/julia/blob/95cdf9b238f320862c74129490b0d06fff1a3926/base/arrayshow.jl#L108), so your 2-argument `show` is not being called. (This was apparently changed in [julia#34387](https://github.com/JuliaLang/julia/pull/34387), which maybe should have gotten more discussion, grr.)

Also, as [commented here](https://github.com/JuliaLang/julia/pull/54547#issuecomment-2125494885), the `:compact=>true` flag is only set when printing multiple columns. You could use `get(io, :typeinfo, Any) === MyType` instead, for example.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [May 27, 2024, 10:39am UTC](https://discourse.julialang.org/t/print-things-differently-in-an-array/114563/10 "2024-05-27T10:39:40Z")

</div>

🎉 that seems to do the trick

```julia
struct MyType
    foo
    bar
 end
 
 m = MyType(1, 2)
 
 function Base.show(io::IO, ::MIME"text/plain", m::MyType)
          if get(io, :typeinfo, Any) === MyType
            print(io, m.foo)
          else
            Base.show_default(io, m)
          end
 end
 
 julia> m
 MyType(1, 2)
 
 julia> [m]
 1-element Vector{MyType}:
  1
```
