One of the major elements that sets Elixir apart from most other programming languages is  immutability. But what does that  actually mean? What does

Serialization is the Secret - Zach Daniel

submited by
Style Pass
2024-09-29 13:30:01

One of the major elements that sets Elixir apart from most other programming languages is immutability. But what does that actually mean? What does it do for us?

The word immutable is defined by Merriam-Webster as “not capable of or susceptible to change”. If nothing ever observes a change, it may as well have not happened anyway, so we will be discussing mutation as two discrete concepts: mutation, and the observation of mutation.

They look very similar. And in fact, they both have consistent behavior if we next line of the program was to print out the value of counter, we are guaranteed in both cases to see the value 1 printed. It would appear that the observation of variables behaves the same.

Before I explain the benefits of immutability, it’s important to disambiguate something that folks often initially believe is mutation.

In the Javascript example, our reference to the counter is mutated . However, in the Elixir example, what we’ve done is rebound the variable. No pointers or values have changed. In Javascript, primitive values are immutable, but a variable’s reference is not. In later examples we’ll see how Javascript allows mutating other values i.e objects, called reference values.

Leave a Comment