<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
On 10/10/2012 06:40 PM, Charles Hixson wrote:
<blockquote cite="mid:5076238D.1080004@earthlink.net" type="cite">
<meta http-equiv="Context-Type" content="text/html;
charset=ISO-8859-1">
On 10/10/2012 04:21 PM, Michael Truog wrote:
<blockquote cite="mid:507602F9.7080005@gmail.com" type="cite"> On
10/10/2012 03:55 PM, Charles Hixson wrote:
<blockquote cite="mid:5075FCF2.6060502@earthlink.net"
type="cite">I'm choosing a language to implement a ... well,
neural network is wrong, and so is cellular automaton, but it
gives the idea. Anyway, I'm going to need, in each cell, a
few stateful items, e.g. activation level. <br>
<br>
When I look at what Erlang can do, I see that the Process
Dictionary looks as if it would serve my needs, but then I am
immediately warned not to use it, that it will cause bugs.
These stateful terms will not be exported from the cell within
which they are resident. Is this still likely to cause
problems? Is there some better approach to maintaining
state? (I can't just generate a new process, because other
cells will need to know how to access this one, or to test
that it has been rolled out.) <br>
<br>
</blockquote>
<tt>This explains some basics about the process dictionary: </tt><a
moz-do-not-send="true"
href="http://www.erlang.org/course/advanced.html#dict">http://www.erlang.org/course/advanced.html#dict</a><br>
Quoted below:<br>
<ul>
<li>Destroys referential transparency </li>
<li>Makes debugging difficult </li>
<li>Survives Catch/Throw</li>
</ul>
So, it is much better to use variables, so side-effects are more
explicit (i.e., function variables). This is the equivalent to
the State variable of a gen_server behaviour (<a
moz-do-not-send="true"
href="http://www.erlang.org/doc/man/gen_server.html">http://www.erlang.org/doc/man/gen_server.html</a>).
Depending on the expected state-handling, you might want a
gen_server, a gen_event, or a gen_fsm for each cell. Otherwise,
if you want to avoid OTP behaviour usage, you could just do
plain Erlang code, but your code might then be more error-prone
(especially since you are asking this question).<br>
<br>
</blockquote>
Thank you. That confirms the recommendation against using the
process directory, though I will admit that I can't see any way
that your proposed alternatives could replace it.<br>
<br>
</blockquote>
Usually the State variable in the OTP behaviours is always a record,
which is preprocessing syntactical sugar for tuples (similar to a
struct in C, conceptually). So, it provides constant time (O(1))
access to tuple elements based on field names (when reading
elements, setting elements has more overhead... all memory is copied
since variables are immutable in Erlang). If you need dynamic field
names, there are many options for various key-value data
structures. In OTP, dict, gb_trees, orddict (and array if your key
type is always an integer). If you need to use dynamic strings as
key values, I have a trie here <a class="moz-txt-link-freetext" href="https://github.com/okeuday/trie">https://github.com/okeuday/trie</a>.
However, it is best (most typical and maintainable) if you can rely
on a record (tuple) for process state. Within the state record, you
can always define any dynamic lookups you might need or other data
structures you wish to utilize.<br>
<br>
Hope this helps make it clear that the key-value data structures
available in separate modules (either within OTP or external to OTP)
help to make sure you can create key-value lookups within Erlang
code without utilizing the process dictionary.<br>
<br>
The other direction you can go is to use ets which provides global
storage in Erlang, managed by a process. However, it is best to
avoid global variables whenever possible.<br>
<br>
<br>
<br>
</body>
</html>