When should I use process-wide dictionary and when should my process state be in the parameters of the loop function?
This:
loop() ->
receive
{From, subscribe} ->
put(listeners, [From|get(listeners)]),
?MODULE:loop()
end.
Or this:
loop(Listeners) ->
receive
{From, subscribe} ->
?MODULE:loop([From|Listeners])
end.
?
Parameters to the loop function has the benefit of being explicit in that nothing outside it can change the parameters (unless you’re doing strange magic bouncing on another function like a trampoline), but state tends to add up, and the parameters with them.
How about a dict as a parameter? Best of both worlds or worst of both worlds?
I would suggest you to use a record to store your loop data. That way if you ever need to add a new “variable” to your loop data all you have to do is add it to the record, without touching any of your clauses. Dict would only be a better choice if your “keys” recently change, which is unlikely in the case of loop data.