[erlang-questions] Function to print a record with fieldnames?

Mats Cronqvist mats.cronqvist@REDACTED
Tue Mar 4 15:53:02 CET 2008


Convey Christian J NPRI wrote:
> I've got a record definition along the lines of:  -record(myrec, {foo = undefined, bar=undefined}).
>
> To help with debugging, I'd like a function that will print out something like the following text:
>    #record{foo = 42, bar = the_answer_to_the_question}
>
> Does such a function exist, or must I write my own version of the function for each record type?  I know I can just use io:format("~p~n", [ MyRecord ]), but I'd really like the fieldnames to also be printed.
>   
 best i've seen is variations on this theme;

-module('foo').
-author('Mats Cronqvist').
-export([go/0]).

-define(rec_info(T,R),lists:zip(record_info(fields,T),tl(tuple_to_list(R)))).
-record(r,{bla,foo,baz,bar}).

go() -> ?rec_info(r,#r{}).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: mats_cronqvist.vcf
Type: text/x-vcard
Size: 179 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080304/b52b1ac8/attachment.vcf>


More information about the erlang-questions mailing list