Why can't I use variables to denote members of a record?

Tony Arcieri <>
Sun Nov 29 00:53:01 CET 2009


I ran into a case where it would really be helpful to do:

Rec#some_record{Member = Value}

And the Erlang preprocessor bitches:

field 'Member' is not an atom or _ in record some_record

I see no reason why this case can't be handled by the Erlang preprocessor
with a case expression.  Specifically this can be exploded into:

case Member of
  member1 -> Rec#some_record{member1 = Value};
  member2 -> Rec#some_record{member2 = Value};
  ...
  memberN -> Rec#some_record{memberN = Value}
end

Then just do the normal preprocessor transforms for those records.  You can
handle multiple Member=Value assignments by folding over all of them with
the same case logic.

Am I the first one in the history of Erlang to desire this behaviour?  As is
I'm manually exploding it into a case expression myself, which means
whenever I want to add a member to the original record I also have to update
my case statement, and the list of the members is spelled out twice.

For a bit more clarification on what I'm doing, I'm building a record from a
list which contains both individual atoms and 2-tuples.  The behavior of the
individual atoms in the list is special cased, but the behavior when a
2-tuple is encountered is to treat them as {Member, Value} and set Member to
Value accordingly.

-- 
Tony Arcieri
Medioh/Nagravision


More information about the erlang-questions mailing list