[erlang-questions] record updating with several functions

Alain O'Dea alain.odea@REDACTED
Wed Jan 19 19:40:52 CET 2011


On 2011-01-19, at 14:16, "info" <info@REDACTED> wrote:

> Hello,
> Given one record with multiple fields.
> Each fields will be updated by executing a lot of functions.
> How to pick up the fields in the main program ?
> Shall I pass as parameter the record ? probably yes.
> How to have the new state of the record after each calls ?
> 
> -record (fields,{ch1,ch2,...}).
> main() ->
> fctA,    %set ch1
> fctB,    %set ch2...
> display the field ch1,ch2,...
> .
> 
> Other case: the function are cascaded (fctA call fctB which call fctC etc ...)
> 
> -record (fields,{ch1,ch2,...}).
> main() ->
> fctA,    %set ch1
> ...
> display the field ch1,ch2,...
> .
> fctA()->
> ... %set ch1
> fctB()
> .
> fctB()->
> ... % set ch2
> fctC()
> .
> 
> J-Ph. Constantin
> ITS3 Genève
> www.its3.ch

Listing of set_record_fields.erl:

-module(set_record_fields).
-record (fields,{ch1,ch2,...}).
main() ->
    Record = fctB(fctA(#fields{})),
    io:format("~w~n"
            "ch1 is ~w~n"
            "ch2 is ~w~n",
            Record, 
            Record#fields.ch1,
            Record#fields.ch2).

fctA(Record) ->
    Record#fields{ch1 = "test ch1"}.

fctB(Record) ->
    Record#fields{ch2 = "test ch2"}.

That should work (untested, my apologies if it doesn't).  I wrote it from bed on my iPhone :)


More information about the erlang-questions mailing list