5

I am learning Elixir, up to chapter 7 PragProg book, and after thinking about the immutability and other items, I was thinking it was not generally possible to create a circular reference in the Elixir Maps/Tuples/Lists, etc.. Where A -> B -> C -> A.

Without going into really trying to trick the system, is this true?

Daniel
  • 7,006
  • 7
  • 43
  • 49
  • I found this in a post by Robert Virding [in the mailing list](http://erlang.org/pipermail/erlang-questions/2008-July/036446.html) "Erlang is suited for reference counting as it has no circular structures, well it has a few circular references but they are only in well-known places so no real problems." I guess you could request him for details (or for an answer here). :) – Dogbert Dec 13 '16 at 08:20
  • You may want to modify your question a bit; I'm not sure that Maps, Tuples and Lists can be considered "equal" in the context of circular references. They're certainly not equivalent in other senses. You may want to narrow your question. – Onorio Catenacci Dec 14 '16 at 18:54

1 Answers1

10

Due to the immutability, there is a chicken-egg problem creating circulars. In fact, there is nothing done by Elixir to prevent it; it just comes out of the box within the immutability.

Proof: Since C in your chain links A on creation, A must exist in advance; A in turn links B hence B must exist even before; B links C, requiring C to exist, but it is not yet created. QED.

One can not simply refer an unexisting Term*, and can not modify the existing one, therefore, it is impossible in Elixir.


* please read the discussion in comments on wording here and why Term was finally chosen.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160