[erlang-questions] Record field position number?
Dmitry Demeshchuk
demeshchuk@REDACTED
Fri Mar 9 16:23:31 CET 2012
#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
More information about the erlang-questions
mailing list