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

Brentley Jones <>
Sun Nov 29 01:39:22 CET 2009


If I remember correctly, all previous requests for this type of feature ended with
someone saying "use setelement".

Though, I also would like this type of functionality. The problem is that Erlang
doesn't know what Member is equal to until runtime, and in that case it needs
the case statement like you mention. Maybe that is a good trade off.

- Brentley Jones

On Nov 28, 2009, at 5:53 PM, Tony Arcieri wrote:

> 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