[erlang-questions] Process scope variable

Tue Feb 17 22:40:05 CET 2015

> Does anyone see any benefit from process scope variables?

it is called "process dictionary" and it is a profound source of bugs 
and pain.
it is useful in some cases, but coding is easier and cleaner without it.

a living example, how do i use the process dictionary:

module "game.erl" represents a game process, few player processes are 
talking to the game process.

init( PlayerNames, PlayerRatings, PlayerPIDs ) ->
   % store useless info in the process dictionary for future use
   put( 'players', PlayerRatings, PlayerNames )
   % store useful info in the stack
   main_loop( Status, PlayerPIDs )

main_loop( bla bla bla ) -> ... ;

% this is the end of the game
% the only place we actually need those PlayerRating and stuff
main_loop( Status = 'end_of_game', PlayerPIDs ) ->
   PlayerInfo = get( 'players' )
   update_player_ratings( PlayerInfo )

as you see i decided to use process dictionary for "long-term" storage 
of the information that is NEVER ALTERED during the lifetime of the 
process -- seems to me quite appropriate usecase for the process dictionary.

but i am not an Erlang guru, i am just using common sense.
similarly i use the process dictionary in genetic algorithms for storing 
a process's genome. it seemed to me totally natural -- use the global 
scope of the process for storing some INTRINSIC process's data.

More information about the erlang-questions mailing list