what is wrong with this code setting a nested record?

Håkan Stenholm hakan.stenholm@REDACTED
Thu Jul 21 02:39:31 CEST 2005


MEENA SELVAM wrote:

>Hi,
>
>the parameter A is of record type arg, which contains
>a subrecord of type, headers and which has a field
>host:
>
>The host field initially contains www.msn.com, and I
>am trying to overwrite with 47.80.18.95, but the
>following code doesnt work.. it still has www.msn.com
>itself.
>
>display_login2(A, ReplyMsgs, URL, IP, FromLogout) ->
>
>    (A#arg.headers)#headers{host="47.80.18.95"},
>
>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="47.80.18.95"},
          ...
          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 =====================

-module(test).

-export([test/0]).

-record(foo, {
         bar
         }).

-record(bar, {
          a,
          b
         }).

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]),

    ok.

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

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

>meena
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around 
>http://mail.yahoo.com 
>
>  
>




More information about the erlang-questions mailing list