# Custom fancy printing of non-empty Dict?

**URL:** https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127
**Category:** General Usage
**Created:** [February 13, 2024, 12:35am UTC](https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127 "2024-02-13T00:35:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GPhMorin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gphmorin/32/206979_2.png) [@GPhMorin](https://discourse.julialang.org/u/GPhMorin)
#### Post date: [February 13, 2024, 12:35am UTC](https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127/1 "2024-02-13T00:35:14Z")

</div>

For a project, I would like the output of a Dict (and, more specifically, an OrderedDict) _containing a type specific to my project_ to be a custom fancy print instead of an overview of the dictionary’s contents.

When I apply this function:

```julia
function Base.show(io::IO, dictionary::Dict{Int64, SpecificType}())
    print(io, "A dictionary containing ", length(dictionary), " items.")
end

```

However, this function works only when using print(dictionary) or show(dictionary), or when the dictionary is empty. Otherwise, when the output is a non-empty dictionary, the print is:

`Dict{Int64, SpecificType} with x entries:`

Is there a way to change this behavior?

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 13, 2024, 1:02am UTC](https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127/2 "2024-02-13T01:02:45Z")

</div>

If you want an `AbstractDict` that prints differently it might be best to make your own `MyDict` struct, perhaps containing a `d::Dict` field and forwarding methods to it, but defining its own special `show` method.

---

<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: [February 13, 2024, 1:08am UTC](https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127/3 "2024-02-13T01:08:32Z")

</div>

I agree with @jar. It’s better to create your own `AbstractDict` that wraps a regular `Dict`.

That being said, the behavior you are observing is odd, and I can’t explain it. Here’s a complete example:

Setup:

```julia
struct Foo end

function Base.show(io::IO, d::Dict{Int64, Foo})
    println(io, "A dictionary containing ", length(d), " items.")
end

d = Dict(1 => Foo());

```

Printing:

```julia-repl
julia> print(d)
A dictionary containing 1 items.

julia> show(d)
A dictionary containing 1 items.

julia> Dict{Int, Foo}()
A dictionary containing 0 items.

julia> d
Dict{Int64, Foo} with 1 entry:
  1 => Foo()

```

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [February 13, 2024, 1:27am UTC](https://discourse.julialang.org/t/custom-fancy-printing-of-non-empty-dict/110127/4 "2024-02-13T01:27:14Z")

</div>

You need to implement the 3-arg `show`. Continuing from @CameronBieganek’s MWE:

```julia
julia> function Base.show(io::IO, ::MIME"text/plain", d::Dict{Int64, Foo})
           println(io, "A dictionary containing ", length(d), " items.")
       end

julia> d
A dictionary containing 1 items.

```
