# Creating a custom Dict clone for formatted printing?

**URL:** <https://discourse.julialang.org/t/creating-a-custom-dict-clone-for-formatted-printing/92975>\
**Category:** General Usage\
**Tags:** type, dict\
**Created:** [January 15, 2023, 1:24am UTC](https://discourse.julialang.org/t/creating-a-custom-dict-clone-for-formatted-printing/92975 "2023-01-15T01:24:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daharn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daharn/32/27088_2.png) [@daharn](https://discourse.julialang.org/u/daharn)\
**Post date:** [January 15, 2023, 1:24am UTC](https://discourse.julialang.org/t/creating-a-custom-dict-clone-for-formatted-printing/92975/1 "2023-01-15T01:24:25Z")

</div>

I want to be able to `print` the key-value pairs from a `Dict`-like object in a certain formatted way.

I see two ways to do this: Either write a `my_print(dct::Dict)` function that does this and use a regular `Dict` to store the pairs. Or create a new type `MyDict` that is essentially a `Dict` with a different name and extend `Base.print` with a method for this new type `print(dct::MyDict)`. Since I will have to create some more custom types for which I want to define a `print` method anyway, I considered the latter.

But I still want `MyDict` to behave like a `Dict`. To achieve this, do I essentially have to copy-paste all the implementation code of `Dict` for `MyDict`, especially the constructors? Or is there a more clever way to do this? Or is this a bad idea in the first place and I should just define a plain old function?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 16, 2023, 10:13am UTC](https://discourse.julialang.org/t/creating-a-custom-dict-clone-for-formatted-printing/92975/2 "2023-01-16T10:13:00Z")

</div>

There are a number of potential printing customizations you may be able apply without creating a new object: see, for example the docstring for `IOContext`. I often find customizing `:typeinfo` to be useful.

A second (perhaps less obvious) option is to create a custom `<:IO` object, and define `show(io::MyIO, d::Dict)` methods. This allows you to customize display without piracy; in practice you might call `print(MyIO(stdout), d)` to display your `d::Dict`, or define `const mystdout = MyIO(stdout)` in your `startup.jl` file and then you can say `print(mystdout, d)` in the REPL.

But if neither of these is attractive, then yes, I would create a new object just for display purposes. Sometimes a very “thin” wrapper suffices, essentially providing a tiny bit of functionality and allowing me to create a custom `show` method. Other times you need something much more extensive. If you create your modified `Dict`, consider making it a subtype of `AbstractDict`: you might get a lot of methods for free that way.

---

<div class="post-metadata">

**Author:** ![daharn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daharn/32/27088_2.png) [@daharn](https://discourse.julialang.org/u/daharn)\
**Post date:** [January 19, 2023, 11:54pm UTC](https://discourse.julialang.org/t/creating-a-custom-dict-clone-for-formatted-printing/92975/3 "2023-01-19T23:54:27Z")

</div>

Thanks for the suggestions!

I probably was not very on point with my us of `print`: While a formatted printout in the REPL is definitely used for debugging purposes, the eventual goal is to write the contents of the object to a text file with a certain formatting. This includes some pre- and postamble. For dealing with the object in Julia, I would be most interested in retaining the interface of a `Dict`. I suppose that would constitute a kind of wrapper type, similar to what is discussed [here](https://stackoverflow.com/questions/58779529/creating-custom-types-in-julia).

But I suppose the underlying question for me is even more general:  
Is there a consensus in the Julia ecosystem which approach is more “natural”:

- define custom types to save data and add methods (potentially for already existing functions) that specifically dispatch on them, which I would consider the more OO-leaning way since it basically ties data to its methods via Type.
- just save the data in standard types and write new functions that work with these standard types, which I would consider a more functional approach.

Is there a rule of thumb when to create custom types? In my example, the desired print/write function would probably work for every `Dict` but it would only _make sense_ for `Dict`s with a specific content.
