[erlang-questions] *current* value?

Gerd Flaig <>
Thu Oct 18 03:16:42 CEST 2007


YC <> writes:

> Basically - one can bind the value by a particular context (I think in
> Erlang process is the basic unit for a context).  The main use is to reduce
> the # of parameters that needs to be explicitly passed in, which obviously
> makes the function impure (although can be more practical).

to the contrary, the function becomes impure if it has side effects,
like e.g. modifying the process dictionary.

The number of parameters passed for the current state is normally
exactly 1. If the state has structure, most of the time records are
used.

% create record with some default values
-record(state, {current_confusion=none, current_answer=42}).

start() ->
    % initial state record is constructed with defaults
    server_loop(#state{}).

server_loop(State) ->
    receive
        {get_confused, Confusion} ->
            server_loop(State#state{current_confusion=Confusion});
        {set_answer, Answer} ->
            server_loop(State#state{current_answer=Answer})
    end.


HTH

        Goodbyte, Gerd.
-- 
The last thing one knows in constructing a work is what to put first.
                -- Blaise Pascal




More information about the erlang-questions mailing list