# Short-form printing not working in vectors

**URL:** https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520
**Category:** General Usage
**Tags:** question
**Created:** [September 4, 2023, 10:50pm UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520 "2023-09-04T22:50:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 4, 2023, 10:50pm UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/1 "2023-09-04T22:50:52Z")

</div>

I’m trying to define short-form and long-form printing for a custom type. It seems to work as expected except when I put objects of the custom type in a vector. Here’s the code:

```julia
struct Foo
    x::Int
    y::Int
end

function Base.show(io::IO, ::MIME"text/plain", f::Foo)
    print(io, "Foo: {", f.x, ", ", f.y, "}")
end

function Base.show(io::IO, f::Foo)
    print(io, "{", f.x, ", ", f.y, "}")
end

```

Long-form printing works:

```julia
julia> Foo(1, 2)
Foo: {1, 2}

```

And short-form printing works in a tuple or pair:

```julia
julia> (Foo(1, 2), Foo(3, 4))
({1, 2}, {3, 4})

julia> Foo(1, 2) => Foo(3, 4)
{1, 2} => {3, 4}

```

But short-form printing is not working in a vector:

```julia
julia> [Foo(1, 2), Foo(3, 4)]
2-element Vector{Foo}:
 Foo: {1, 2}
 Foo: {3, 4}

```

Does anyone know what is going on here? I thought that vector printing is supposed to use the short-form, as [the example in the manual](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) shows.

(Code tested on v1.8.2)

```julia
julia> VERSION
v"1.8.2"

```

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 4, 2023, 11:46pm UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/2 "2023-09-04T23:46:25Z")

</div>

The short form is used with `print` or `println` for a vector:

```
julia> println([Foo(1, 2), Foo(3, 4)])
Foo[{1, 2}, {3, 4}

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 5, 2023, 12:06am UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/3 "2023-09-05T00:06:35Z")

</div>

I dunno, the example in the manual works as expected in the REPL without calling `print` or `println` on the vector:

```julia
julia> Polar(3, 4.0)
Polar{Float64} complex number:
   3.0 * exp(4.0im)

julia> [Polar(3, 4.0), Polar(4.0,5.3)]
2-element Vector{Polar{Float64}}:
 3.0 * exp(4.0im)
 4.0 * exp(5.3im)

```

The only difference I can see between my example and the example in the manual is that my long-form version only prints one line, whereas in the example in the manual the long form prints multiple lines. However, I really wouldn’t think that that would affect which `show` method on `Foo` is getting called when I print a vector of `Foo`.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 5, 2023, 8:35pm UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/4 "2023-09-05T20:35:38Z")

</div>

What would be the workaround here?

Would something like this be alright:

```julia
function Base.show(io::IO, ::MIME"text/plain", v::Vector{Foo})
  println("$(length(v))-element $(typeof(v))")
  foreach(f->println(io, f), v)
end

julia> [Foo(1, 2), Foo(3, 4)]
2-element Vector{Foo}
{1, 2}
{3, 4}

```

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 5, 2023, 9:01pm UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/5 "2023-09-05T21:01:02Z")

</div>

> [@rafael.guerra](#):
>
> What would be the workaround here?

I was hoping somebody would point out something subtle that I’m doing wrong. 🙂

Or if it’s actually a bug, then I should file a github issue.

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [September 6, 2023, 3:12am UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/6 "2023-09-06T03:12:33Z")

</div>

> [@CameronBieganek](#):
>
> The only difference I can see between my example and the example in the manual is that my long-form version only prints one line, whereas in the example in the manual the long form prints multiple lines.

But that’s exactly it; if the 3-argument `show` makes a one-liner, it’s used for arrays too. The logic is in `Base.print_matrix_row()`.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [September 6, 2023, 4:31am UTC](https://discourse.julialang.org/t/short-form-printing-not-working-in-vectors/103520/7 "2023-09-06T04:31:51Z")

</div>

Aw, shucks. I had hoped that the array printing was based purely on dispatching the 2-argument `show`, rather than checking the 3-argument `show` output for a newline character. Basing the printing on dispatch would be a lot more predictable. In fact, the manual strongly implies that the 2-argument `show` is called when printing array elements:

> In particular, sometimes one wants both a verbose multi-line printing format, used for displaying a single object in the REPL and other interactive environments, and also a more compact single-line format used for [`print`](https://docs.julialang.org/en/v1/base/io-network/#Base.print) or for displaying the object as part of another object (e.g. in an array).

> This results in: […] where the single-line `show(io, z)` form is still used for an array of `Polar` values.
