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:
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:
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.
You need to implement the 3-arg show. Continuing from @CameronBieganek’s MWE:
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.