[erlang-questions] Record field position number?

Max Bourinov bourinov@REDACTED
Fri Mar 9 16:20:45 CET 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120309/7787e2ce/attachment.htm>


More information about the erlang-questions mailing list