[erlang-questions] record with dynamic item name

Motiejus Jakštys desired.mta@REDACTED
Mon Aug 29 09:30:01 CEST 2011


On Sun, Aug 28, 2011 at 12:46:10PM +0200, Roberto Ostinelli wrote:
> Hi list,
> 
> is there any way to retrieve a record item whose identifier is in a
> variable? For instance:
> 
> -record(state, {
>     type = 1
> }).
> 

Ulf Wiger has done this job in parse_trans package:
https://github.com/uwiger/parse_trans

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

Basically looks like this:
-compile({parse_transform, exprecs}).
-export_records(['r']).
-record(r, {a, b}).

imho(MyField) ->
    R = r{a = yadda, b = yabba},
    '#get-'(MyField, R).

main() ->
    imho(a). %% Returns yadda

Motiejus



More information about the erlang-questions mailing list