SV: pretty printing of records

Lennart Öhman <>
Wed Aug 10 10:04:23 CEST 2005


Hi,
the main "problem" is of course that record-field names are
not available in run-time, unless *you* store them some place.
The nice rr functionality in the shell then of course reads
a record definition and stores it "somewhere", available to
the shell process. And then formats any tuple suspected of
being that record accordingly instead of plain tuple format.
 
What you want to do is pretty simple to accomplish in your
own program. But you must have run-time access to field-names
and their respective location. This can of course be done by
using standard record constructs such as record_info(fields,RecordType)
(which in compile time will expand to the list of all fields
in their correct order).
 
I would make a function which returns a string looking like
a record (#myrecord{field1=foo,field2=bar}) and then call
 
io:format("~s",[record2string(Record,record_info(fields,myrecord))])
 
Since the record_info construct will expand to a list in compile
time. It might be of interest, if .beam sizes is of importance
and their will be a lot of calls to record2string/2,
to make a more clever record2string function which instead
has all record-field lists expanded once and for all, taking
just the record-type as argument.
 
Best Regards,
Lennart
 
 
-------------------------------------------------------------
Lennart Ohman                   phone   : +46-8-587 623 27
Sjöland & Thyselius Telecom AB  cellular: +46-70-552 6735
Sehlstedtsgatan 6               fax     : +46-8-667 8230
SE-115 28 STOCKHOLM, SWEDEN     email   : 

________________________________

Från:  genom Mikael Karlsson
Skickat: on 2005-08-10 08:22
Till: 
Ämne: pretty printing of records



I have noticed the nice erlang shell commands rr() and rp()
to display records with attribute names and all.
Is there a way to do something similar directly from a
program, using io:format("..",[Record]) for instance?

Mikael



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20050810/931d6865/attachment.html>


More information about the erlang-questions mailing list