[erlang-questions] proposal: pretty printing through polymorphism

Scott Parish <>
Fri May 16 04:59:44 CEST 2008


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



More information about the erlang-questions mailing list