[erlang-questions] Record field position number?

Max Bourinov bourinov@REDACTED
Fri Mar 9 16:31:23 CET 2012


Thanks. It was heavy Friday for me.


Best regards,
Max




On Fri, Mar 9, 2012 at 6:23 PM, Dmitry Demeshchuk <demeshchuk@REDACTED>wrote:

> #recordname.fieldname
>
> On Fri, Mar 9, 2012 at 7:20 PM, Max Bourinov <bourinov@REDACTED> wrote:
> > Hi Erlangers,
> >
> > Is there is a way to find a position number of the field in the record:
> >
> > 1. At compile time (record_info)?
> > 2. At runtime (I think not).
> >
> > Motivation:
> > I use lists module and its functions like lists:keyfind/3 for dealing
> with
> > records. I don't want to hardcode number to target certain record's
> field. I
> > afraid that if I change order of fields in my records my code will be
> broken
> > (It will be indeed). So, I do believe that it is possible to do something
> > like:
> >
> > ...
> > -record(my_rec {
> >   name, age, gender
> > }).
> > %
> > ListOfRecords = [#my_rec{name = "John"}, #my_rec{name =
> > "Peter"}, #my_rec{name = "Marry"}],
> > Value = "Peter",
> > %
> > case lists:keyfind(Value,
> ?get_filed_number(name, my_rec), ListOfRecords) of
> > ...
> >
> > I hope it is possible to define some sort of makro that will do the
> job...
> >
> > Any ideas?
> >
> > Best regards,
> > Max
> >
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
>
>
>
> --
> Best regards,
> Dmitry Demeshchuk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120309/97afe445/attachment.htm>


More information about the erlang-questions mailing list