Erlang/OTP Forums

Author Message

<  User Contributions  ~  parse_transform - exporting record info

uwiger
Posted: Thu Sep 28, 2006 4:54 pm Reply with quote
User Joined: 03 Jul 2006 Posts: 567 Location: Sweden
An earlier version of this module was posted in erlang-questions here: http://www.erlang.org/ml-archive/erlang-questions/200605/msg00377.html

The module is a parse transform allowing you to export records. The transform adds accessor functions for instantiating, inspecting and modifying records, without having to introduce compile-time dependencies between modules.

I've added an introspective function:
Code:
'#info-'(Rec) -> [FieldName]
which doesn't require you to know which type of record it is. The following example program illustrates how this can be used for a generic pretty-print function for your records.

Code:
-module(test3).

-export([pp/1]).

-compile({parse_transform, exprecs}).

-record(r1, {a, b, c}).
-record(r2, {a1, a2}).
-record(r3, {f1, f2}).   % note: this record is not exported

-export_records([r1,r2]).

pp(Rec) ->
    RF = fun(R,L) when R == element(1,Rec) ->
       Flds = '#info-'(Rec),
       true = (L == length(Flds)),
       Flds
    end,
    io:fwrite([io_lib_pretty:print(Rec, RF),"\n"]).


Code:
1> R = test3:'#new-r1'([{a,1},{b,2}]). 
{r1,1,2,undefined}
2> test3:'#get-r1'(b, R).
2
3> test3:pp(R).
#r1{a = 1,b = 2,c = undefined}
ok
4> R2 = test3:'#new-r2'().
{r2,undefined,undefined}
5> test3:pp(R2).         
#r2{a1 = undefined,a2 = undefined}
ok
6> test3:'#new-r3'().     

=ERROR REPORT==== 28-Sep-2006::18:51:26 ===
Error in process <0.254.0> with exit value: {undef,[{test3,'#new-r3',[]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}

** exited: {undef,[{test3,'#new-r3',[]},
                   {erl_eval,do_apply,5},
                   {shell,exprs,6},
                   {shell,eval_loop,3}]} **



The last call failed since we didn't export the record r3.

/Ulf Wiger



exprecs.erl
 Description:
exprecs.erl version 0.3

Download
 Filename:  exprecs.erl
 Filesize:  11.04 KB
 Downloaded:  978 Time(s)

View user's profile Send private message Visit poster's website
uwiger
Posted: Thu May 03, 2007 9:28 am Reply with quote
User Joined: 03 Jul 2006 Posts: 567 Location: Sweden
I've fixed a minor bug, and also included some generic accessors:

Code:

'#get-'(A, Rec) -> Value | [Value].
'#set-'(Data, Rec) -> NewRec.


These two functions will detect the type of record and call the appropriate get/set accessor functions.

This makes it easier to write generic code such as

Code:

case Dictionary:'#get-'('Destination-Host', DiameterMsg) of
    [] ->
        ...;
    [DestHost] ->
        ...
end


I've also made an incompatible change:

Code:

'#info-R'(Type) -> Info.   % Type :: fields | size


Previously, 'fields' was implicit.

The module contains edoc documentation.



exprecs.erl
 Description:
exprecs-0.4

Download
 Filename:  exprecs.erl
 Filesize:  14.09 KB
 Downloaded:  779 Time(s)

View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
Post new topic

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum