[erlang-questions] proposal: pretty printing through polymorphism

Ulf Wiger (TN/EAB) <>
Wed May 21 09:20:05 CEST 2008


Eric Merritt skrev:
> I think this is probably worth more then simple shell
 > printing. Having usefully printed data structures in
 > logs, etc would probably be pretty useful. Though
 > implementing it all over the place would probably be
> non-trivial.

The shell pretty-printing could be generalized, though.
It uses a function called io_lib_pretty:print/5, and
if there were an easy way to call on a library function
that automatically uses the pretty-printing functions
loaded into the shell, it could be used from anywhere.

One annoying part of this is trying to find the shell
instance. I wrote about this in my gproc paper, partly
as a small step in this direction:

http://svn.ulf.wiger.net/gproc/doc/erlang07-wiger.pdf

Another alternative is of course to lift the implementation
of custom pretty-printing out of the shell module and
making a general library out of it. It would be reasonable
to also extent ADT modules with a pretty_print() hook,
but the thing that the shell pretty-printing does is
also to recognize an ADT when it appears inside a larger
structure.

Another area where ROK's abstract patterns would come in
handy, perhaps?  (:

BR,
Ulf W


> 
> On Mon, May 19, 2008 at 1:54 AM, Ulf Wiger (TN/EAB)
> <> wrote:
>> A while back, I hacked the Erlang shell in order to
>> allow custom pretty-printing:
>>
>> http://ulf.wiger.net/weblog/2007/11/20/extending-the-erlang-shell-part-1/
>>
>> You can find the code here:
>>
>> svn co http://svn.ulf.wiger.net/ext_shell/trunk
>>
>> The trunk is for R12B. See also
>>
>> http://svn.ulf.wiger.net/ext_shell/branches/r11b/
>> http://svn.ulf.wiger.net/ext_shell/branches/r12b/
>>
>> Please feel free to play around with it and propose
>> improvements.
>>
>> BR,
>> 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
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>



More information about the erlang-questions mailing list