# IOContext :compact when showing array

**URL:** <https://discourse.julialang.org/t/iocontext-compact-when-showing-array/108163>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 29, 2023, 7:10pm UTC](https://discourse.julialang.org/t/iocontext-compact-when-showing-array/108163 "2023-12-29T19:10:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [December 29, 2023, 7:10pm UTC](https://discourse.julialang.org/t/iocontext-compact-when-showing-array/108163/1 "2023-12-29T19:10:56Z")

</div>

The documentation of [`IOContext`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple%7BIO,%20Pair%7D) says

> `:compact` […] is set when printing array elements.

However, I don’t find this to be true:

```julia
struct MyStruct
    v::String
end

function Base.show(io::IO, val::MyStruct)
    if haskey(io, :compact)
        write(io, "[$(io[:compact])]$(val.v)]")
    else
        write(io, val.v)
    end
end

a = [MyStruct("a"), MyStruct("b"), MyStruct("c")]

```

prints

```julia
3-element Vector{MyStruct}:
 a
 b
 c

```

in the REPL, and ` "$a"` prints as `"MyStruct[a, b, c]"`.

So where does `:compact` actually come into play in normal usage?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [December 30, 2023, 8:38am UTC](https://discourse.julialang.org/t/iocontext-compact-when-showing-array/108163/2 "2023-12-30T08:38:03Z")

</div>

> [@goerz](#):
>
> ```julia
> struct MyStruct
> v::String
> end
> 
> function Base.show(io::IO, val::MyStruct)
> if haskey(io, :compact)
> write(io, "[$(io[:compact])]$(val.v)]")
> else
> write(io, val.v)
> end
> end
> 
> a = [MyStruct("a"), MyStruct("b"), MyStruct("c")]
> 
> ```

It comes into play, for example, for higher dimensional arrays:

```julia-repl
julia> fill(MyStruct("a"), 2, 2)
2×2 Matrix{MyStruct}:
 [true]a] [true]a]
 [true]a] [true]a]

```

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [July 17, 2024, 12:24pm UTC](https://discourse.julialang.org/t/iocontext-compact-when-showing-array/108163/3 "2024-07-17T12:24:57Z")

</div>

Sorry to revive but I also just stumbled over this. Given that the documentation states

> `:compact`: Boolean specifying that values should be printed more compactly, e.g. that numbers should be printed with fewer digits. **This is set when printing array elements.** `:compact` output should not contain line breaks.

and given that `Vector` is an array, isn’t the current behavior for one-dimensional arrays inconsistent with what is stated in the docs?

Irrespective of what it should be, is there currently a way to get the compact show also for a vector of custom elements?
