[erlang-questions] proposal: pretty printing through polymorphism

Eric Merritt <>
Wed May 21 01:52:19 CEST 2008


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.

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