[erlang-questions] mutable state

Erik Søe Sørensen eriksoe@REDACTED
Mon May 12 16:58:39 CEST 2014

It loses the GC in the sense (also explained in the OP) that the GC cannot
detect when an object reference is no longer in use and then respond to
that by cleaning up the process dictionary.

Indeed, while emulating objects with mutable state can be done in a number
of ways in Erlang (using process dict / ETS / processes / resource
binaries),  getting GC into the picture appears to be the trickier part.
There is only (afaik) one reference-tracing mechanism in Erlang - the heap
GC - and that one works only with immutable state.

(Elaborating on what OK said about wrecking the garbage collector by going
behind the VM's back and modifying existing tuples etc.: The Erlang GC is a
generational GC which takes advantage of the fact that old objects can
never refer to younger ones. That invariant makes generational GC easyish
to implement, but breaking that invariant would make the VM crashyish.)

In short - the question is interesting, but Erlang does not supply
GC-for-mutable-objects, so in any case you'd have to bring that - whether a
reference-counting or tracing GC.

2014-05-12 0:16 GMT+02:00 <ok@REDACTED>:

> > 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?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140512/436ee900/attachment.htm>

More information about the erlang-questions mailing list