# Array printing of custom objects

**URL:** https://discourse.julialang.org/t/array-printing-of-custom-objects/111606
**Category:** General Usage
**Tags:** question, show, pretty-printing
**Created:** [March 14, 2024, 8:34am UTC](https://discourse.julialang.org/t/array-printing-of-custom-objects/111606 "2024-03-14T08:34:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mdsalerno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mdsalerno/32/5539_2.png) [@mdsalerno](https://discourse.julialang.org/u/mdsalerno)
#### Post date: [March 14, 2024, 8:34am UTC](https://discourse.julialang.org/t/array-printing-of-custom-objects/111606/1 "2024-03-14T08:34:27Z")

</div>

I defined the following object and show() methods:

```julia
# Custom Set

struct CustomSet{T}
    dict::Dict{T, Nothing}
end

CustomSet(xs) = CustomSet(Dict(zip(xs, fill(nothing, length(xs)))))

# default show used by print
function Base.show(io::IO, cs::CustomSet)
    print(io, "CustomSet($(collect(keys(cs.dict))))")
end

# default show used by display() on the REPL
function Base.show(io::IO, mime::MIME"text/plain", cs::CustomSet)
    print(io, "$(length(cs.dict))-element $(typeof(cs)):\n")
    for i ∈ keys(cs.dict) println(i) end
end

```

The behavior is mostly as I would like it to be:

```julia
julia> s = CustomSet([1,2,3,4])
4-element CustomSet{Int64}:
4
2
3
1

julia> print(s)
CustomSet([4, 2, 3, 1])
julia> (s, s)
(CustomSet([4, 2, 3, 1]), CustomSet([4, 2, 3, 1]))

```

Except when printing an array containing a CustomSet type:

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

julia> [s, s]
2-element Vector{CustomSet{Int64}}:
 4
2
3
1
CustomSet([4, 2, 3, 1])
 4
2
3
1
CustomSet([4, 2, 3, 1])

```

I would like for this to print more like a Set object:

```julia
julia> t = Set([1,2,3,4])
Set{Int64} with 4 elements:
  4
  2
  3
  1

julia> [t, t]
2-element Vector{Set{Int64}}:
 Set([4, 2, 3, 1])
 Set([4, 2, 3, 1])

```

What is the best way to achieve this? Also, is this the typical pattern for defining show() methods for custom objects, or is there a better way?

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [March 14, 2024, 10:02am UTC](https://discourse.julialang.org/t/array-printing-of-custom-objects/111606/2 "2024-03-14T10:02:08Z")

</div>

One small bug is you need the `println` in the loop to print to `io`:

```julia
    for i ∈ keys(cs.dict) println(io, i) end

```

This will fix most of your issues.

You might also want to check if the [IOContext](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext-Tuple%7BIO,%20Pair%7D) has already printed type info so you can avoid having type info for each element of an array.

The spacing is also a little better with something like:

```julia
function Base.show(io::IO, ::MIME"text/plain", cs::CustomSet)
    print(io, "$(length(cs.dict))-element $(typeof(cs)):\n ")
    print(io, join(keys(cs.dict), "\n "))
end

```

The other `IOContext` elements are also useful for displaying very large sets in a REPL-friendly way.
