[erlang-questions] managing large records

Thomas Lindgren thomasl_erlang@REDACTED
Sun May 11 20:35:28 CEST 2008


--- Szabolcs Berecz <szabolcs.berecz@REDACTED> wrote:

> Hi,
> 
> I have a record with a lot of fields which are
> updated nontrivially. I
> tried to create a process for this task which looks
> like this:
> 
> -record(mm, {a=1,b=4,c}).
> 
> new() ->
>         loop(#mm{}).
> 
> loop(Mm) ->
>         receive
>                 {set, Field, Value} ->
>                         loop(Mm#mm{Field=Value});
>                 {get, Pid} ->
>                         Pid ! Mm,
>                         loop(Mm)
>         end.
> 
> But as you probably know, it doesn't work because it
> needs to now the
> value of Field at compile time (or at least, that's
> what I have
> concluded).
> So, my question is: how would you solve the problem
> (lot's of fields,
> almost random updates)?

As mentioned before, dict or similar is probably a
fine choice. Another option is to roll your own:

0. Map field names to integers by hand.

n(field_1) -> 1;
...
n(field_k) -> K;
n(_) -> exit(unknown_field).


1. represent the fields as a tuple Rec of K elements,
with elements starting as undefined (for instance)

2. Read/write the fields using

     element(n(Field), Rec)
     setelement(n(Field), Rec, Value)

Best,
Thomas


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ



More information about the erlang-questions mailing list