[erlang-questions] proposal: pretty printing through polymorphism

Ulf Wiger (TN/EAB) <>
Mon May 19 10:54:39 CEST 2008

A while back, I hacked the Erlang shell in order to
allow custom pretty-printing:


You can find the code here:

svn co http://svn.ulf.wiger.net/ext_shell/trunk

The trunk is for R12B. See also


Please feel free to play around with it and propose

Ulf W

Scott Parish skrev:
> Problem:
> Right now pretty printing values shows the lowest layer data
> structures. This is ugly and usually not what an end user wants/needs
> to see. For instance:
> 1> dict:store(color, red, dict:store(size, 5, dict:new())).
> {dict,2,
>       16,
>       16,
>       8,
>       80,
>       48,
>       {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
>       {{[],[],[[color|red]],[],[],[[size|5]],[],[],[],[],[],[],[],[],[],[]}}}
> Solution:
> The proposed solution would take advantage of the type of polymorphism
> suggested in my prior email. Namely, there would be a function such as
> string:pretty_print/1 which would take a derived datastructure, use
> the record name to call RecordName:pretty_print/1. Things such as the
> shell or io:format could then use this interface to print truely
> pretty versions of derived types (if such is provided).
> Side discussion:
> Actually, a pretty_print would probably need to take more arguments
> then just the data structure; it would probably need to be passed
> information such as the width of the left margin and the wrap length.
> Alternatively, instead of "pretty printing", a form could be returned
> which would represent how to reproduce the data structure. This would
> serve the dual purpose of being much easier for end users to read, as
> well as being immediately copy/paste-able. For an example of what this
> would look like, dict might return: "{dict, from_list, [[{color, red},
> {size, 5}]]}" for my earlier example. io:format (and friends) of
> course could then easily print this as "dict:from_list([{color, red},
> {size, 5}])".
> Thoughts?
> sRp
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list