what is wrong with this code setting a nested record?

Håkan Stenholm <>
Thu Jul 21 02:39:31 CEST 2005


>the parameter A is of record type arg, which contains
>a subrecord of type, headers and which has a field
>The host field initially contains www.msn.com, and I
>am trying to overwrite with, but the
>following code doesnt work.. it still has www.msn.com
>display_login2(A, ReplyMsgs, URL, IP, FromLogout) ->
>    (A#arg.headers)#headers{host=""},
>am i doing anything wrong here?
It should work (see my test code below), but may have forgotten to do:

          NewHeader = (A#arg.headers)#headers{host=""},
          xxx(NewHeader, ...)  %% use the new (updated) Header rather 
than the old one in A

It would be helpful to see a bit more of your code, I get the feeling 
that your trying to do a destructive update on a record in a single 
assignment language, this won't work - data elements in Erlang can only 
be created but never modified[1] (with the exception for 
ets/mnesia/process dictionaries which can conceptually be viewed as 
processes storing data in some regular Erlang data type like a binary 
tree - which is updated by creating new nodes and leaves to replace the 
old ones - rather than overwriting old ones).

[1] : deletion is done by the GC.

================== test.erl =====================



-record(foo, {

-record(bar, {

test() ->
    Bar = #bar{a = 1, b = 2},
    Foo = #foo{bar = Bar},

    io:format("Foo = ~p~n", [Foo]),

    %% create new bar record
    %% access foo record and create new bar record based on foo.bar
    Bar2 = (Foo#foo.bar)#bar{b = new_val},
    io:format("Bar2 = ~p~n", [Bar2]),

    %% create new foo record
    %% create new foo record containing the new Bar2 bar record
    Foo2 = Foo#foo{bar = Bar2},
    io:format("Foo2 = ~p~n", [Foo2]),


================== test.erl =====================

2> c(test).
3> test:test().
Foo = {foo,{bar,1,2}}
Bar2 = {bar,1,new_val}
Foo2 = {foo,{bar,1,new_val}}

>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the erlang-questions mailing list