[erlang-questions] Dynamically access record fields

Pierre Fenoll pierrefenoll@REDACTED
Sun Feb 10 02:02:12 CET 2013


On 9 February 2013 23:57, Dmitry Kolesnikov <dmkolesnikov@REDACTED> wrote:
> Hello,
>
> What I was saying was that you can't do something like
>
> Field = a,
> V2 = setelement(#foo.Field, V1, "A+")
>
>
> but you can do
>
> Field = #foo.a,
> V2 = setelement(Field, V1, "A+")
>
> and do not see any difference, isn't it?
>
> - Dmitry

Well yes there is.
What I think Jeremy wants is to dynamically concatenate record names,
which is as you know
impossible due to record names being resolve at compile-time.

For instance, you can do this
get_foo_a() ->
  element(
    binary_to_term(list_to_binary(
      binary_to_list(term_to_binary(#foo.a)))), M).

However, you cannot do that:
get_foo(Field=a) ->
  element(
    binary_to_term(list_to_binary(
      binary_to_list(term_to_binary(#foo.)) ++ atom_to_list(Field))), M).

* 1: syntax error before: ')'

(#foo. throws the syntax error, so does #foo).

Isn't it a drawback? Is it even good Erlang practice?

I don't want to troll. Is there an EEP about setting record names dynamically?

Regards,
-- 
Pierre Fenoll



More information about the erlang-questions mailing list