[erlang-questions] Access to process dictionary

Vance Shipley vances@REDACTED
Sun Dec 23 14:30:21 CET 2018


On Sun, Dec 23, 2018 at 6:32 PM Donald Steven <t6sn7gt@REDACTED> wrote:
> So I needed to know where in the input buffer I was; that is, to save the buffer index.

Sure, we very often have state we need to maintain. In functional
programming you pass in all the information needed for a function to
perform it's work. Here you would pass the buffer index as an argument
to your function.

> If you have a non-process-dictionary solution, I'm all ears.

Look at the OTP kernel application's disk_log: chunk/2 function:
http://erlang.org/doc/man/disk_log.html#chunk-2

It takes 'Log' and  'Continuation' arguments. The first time it's
called you provide the value 'start' for 'Continuation' and the result
is a tuple of '{Contnuation, Terms}' where Continuation is an opaque
value which you use in the next call to chunk/2. The function is
reentrant because the caller is keeping the state.

Where to keep it? Basically you do the same thing, you keep passing it
in an argument to the functions being executed by your process.  Look
at the OTP stdlib application's gen_server behaviour:
http://erlang.org/doc/man/gen_server.html#Module:init-1

Every function includes a 'State' argument which carries around all
your stateful stuff. This is where your "global variable" type stuff
lives.


-- 
     -Vance



More information about the erlang-questions mailing list