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

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Tue Mar 4 16:29:27 CET 2008


Convey Christian J NPRI skrev:
> Thanks Ulf.
> 
> Perhaps my inexperience with Erlang is hindering me, but from your
> blog I don't see how to employ your work. Specifically, the version of
> the shell I'm using doesn't seem to have the extensions you defined (fa
> / fl / fr), and even if it did, wouldn't I still need to write a new
> pretty-printer extension for each tagged record type?
> 
> Thanks,
> Christian 

Yes, I guess that was a bit over the top, if all you wanted to do was to 
print some debugging output. (:

http://forum.trapexit.org/viewtopic.php?p=21790#21790

Perhaps this contrib is a little bit more along the lines of Mats's good 
advice. BTW, he used a macro because the record_info/2 function is a
"pseudo function", recognized expanded by the pre-processor.

The contribution described above (following the link), a
"parse transform"(*) generates real functions for reading and
writing record data, including metadata such as field
names. The post illustrates how to use this to write a
pretty-printing function.

BR,
Ulf W

(*) Parse transform sounds ominous, but it boils down to
including a .hrl file and making sure that the exprecs.beam
file is in the path when compiling.



> 
>> -----Original Message-----
>> From: Ulf Wiger (TN/EAB) [mailto:ulf.wiger@REDACTED] 
>> Sent: Tuesday, March 04, 2008 9:38 AM
>> To: Convey Christian J NPRI
>> Cc: erlang-questions@REDACTED
>> Subject: Re: [erlang-questions] Function to print a record 
>> with fieldnames?
>>
>> Convey Christian J NPRI skrev:
>>> 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.
>>
>> There is such a function - almost (it's not documented, and 
>> may not do exactly what you want).
>>
>> I wrote about it a while back in my blog:
>>
>> http://ulf.wiger.net/weblog/2007/11/20/extending-the-erlang-sh
>> ell-part-1/
>>
>> BR,
>> Ulf W
>>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list