Updating nested records in a single-line expression?

Peter L erlang@REDACTED
Fri Oct 8 10:19:52 CEST 2004


do you mean:

-module(recs).
-record(arec,{a,b}).
-record(brec,{c,d}).
-export([test/0]).
%%and then...
test() ->
    %%assignment statement
    Arec = #arec{a=hello,b=#brec{c=world,d=again}},

    %% read
    #arec{a=A,b=Brec=#brec{c=C,d=D}} = Arec,

    %%reassign
    Arec2 = Arec#arec{a="hello",b=Brec#brec{c="world",d="again"}},

    io:format("~p\n",[{Arec,Arec2}]).

Tested and works!

best regards,
Peter Lund

> -----Ursprungligt meddelande-----
> Från: owner-erlang-questions@REDACTED
> [mailto:owner-erlang-questions@REDACTED]För Rachwal Waldemar-AWR001
> Skickat: den 7 oktober 2004 21:23
> Till: erlang-questions@REDACTED
> Ämne: Updating nested records in a single-line expression?
>
>
> Hi!
>
> Accessing data from nested records is fairly clear.
> However, I experienced notational problems trying to update
> such records in
> a single line expression.
> Suppose we have a protocol where messages consist of elements
> which in turn
> are collections of fields being simple values or form further nested
> structures.
> When I modify any nested field I have to get a modified copy
> of the message,
> but how to get it in a single expression without additional variables?
> Is it doable?
> Or, maybe I have to deal with such messages in different way
> rather than
> model as records and then access/update these records directly?
>
> Thanks for any help,
> Waldemar.





More information about the erlang-questions mailing list