[erlang-questions] record with dynamic item name

Jon Watte jwatte@REDACTED
Thu Sep 1 02:00:06 CEST 2011


You can also do that using record_info() without using a parse transform.
record_info(name, fields) returns a list of field names, which map to their
position in the tuple (remember that element 1 is the type). Use
element(Tuple, N) to extract the element.

Sincerely,

jw


--
Americans might object: there is no way we would sacrifice our living
standards for the benefit of people in the rest of the world. Nevertheless,
whether we get there willingly or not, we shall soon have lower consumption
rates, because our present rates are unsustainable.



2011/8/29 Motiejus Jakštys <desired.mta@REDACTED>

> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110831/1bee87dc/attachment.htm>


More information about the erlang-questions mailing list