IOContext :compact when showing array

The documentation of IOContext says

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

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

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

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?

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

julia> fill(MyStruct("a"), 2, 2)
2×2 Matrix{MyStruct}:
 [true]a]  [true]a]
 [true]a]  [true]a]
1 Like