[erlang-questions] mutable state

Mon May 12 00:16:24 CEST 2014

> Hi, I'm working on a project that tries to bring a mutable state, OO
> language to the BEAM.  I believe this isn't impossible, but would like
> to ask your opinion on how to map certain OO constructs to Erlang, and how
> to bypass immutable state constraints.

If it were possible for you to do so, you would wreck the garbage
collector, which would be a Very Bad Thing.

Two obvious things to try are
 - the (per-)process dictionary
 - ETS tables.
As for the (per-)process dictionary, each Erlang process
has what is in effect a hash table mapping immutable values
to immutable values.  So you could map
    {Object_Reference, Slot_Name} keys
to  arbitrary *immutable* values.
ETS tables let you do the same thing on a larger scale and
visible to multiple processes.
> The only problem is
> that it's only a single dictionary, but I need at least one mutable
> hash/dict per object in order to hold mutable state.

No, you don't.  You need a replaceable slot per *field*.
And {Object_Reference, Field_Name} pairs give you exactly that.

And of course using the per-process dictionary does NOT lose you
the Erlang GC.  Why would it?

More information about the erlang-questions mailing list