What's the difference between variable binding and assignment?

A couple clarifications:

There is no “assignment” in Elixir[1], when you use ‘=’ you are doing a pattern match and it either succeeds or fails. If it fails, the current process crashes.

The compiler/runtime has a set of rules that it uses to attempt to make the pattern match succeed. One of these is “binding” and “re-binding”. Binding creates a variable that references a term in memory, if that variable does not already exist in the current scope. In Erlang, all further uses of that variable in it’s current scope are fixed to that term in memory. Elixir cheats a bit and allows you to “re-bind”[2] a variable to a new term in memory in certain situations, however the actual term in memory from the original binding does not change and will need to be garbage collected at some point.

[1]- Except where there is… Process Dictionary comes to mind.

[2]- If I am understanding it correctly: Under the hood the Elixir compiler actually creates a new variable name and just remembers that x is really x1 from now on.

2 Likes