Hacker News new | ask | show | jobs
by chrissnell 3220 days ago
Pretty-printing of data structures in a human-readable format.
1 comments

I think Jupyter has the right idea here with various kinds of repr-alternatives that can render html or images directly into the notebook. http://ipython.readthedocs.io/en/stable/config/integrating.h...
This is the kind of use cases where multiple dispatch is useful.
How so? I feel like single dispatch (as in Python) is already enough.
If you write a custom _repr_html_, you are likely to assemble subparts by recursively calling _repr_html_ on sub-elements (thanks to open recursion). If you have an object B which inherits from A, you can specialize on B, or use the existing method for A.

However, you cannot specializes the "html" part with inheritance. Say for example that you want to define a slightly different _repr_html_ method depending on the context (maybe you target the subset of HTML that works correctly in emails) or if you want to render "latex" with custom "tikz" macros instead of using matplotlib.

With multiple dispatch, you can specialize on the target too, which means you can specialize wherever required or fall back to a generic behavior if not.