Immutability Drives Everything
Explore how immutability drives Elixir's functional programming model. Understand variable binding, the illusion of mutability, and how stable data structures improve concurrency and code reliability.
Immutability in Elixir
Functional programming means that the same inputs will give us the same outputs. Elixir binds variables exactly once.
How Elixir feigns mutability
When we say Elixir doesn’t allow mutable variables, we might be tempted to push back. Technically, that’s correct, but we should see the games the compiler is playing to maintain the illusion of mutability. Take a look at this example:
Executable
Output
iex(1)> x = 10
10
iex(2)> x
10
iex(3)> x = 11
11
iex(4)> x
11
That looks like x is mutable, but what we’re seeing is not the whole picture. The values 10 and 11 are immutable. The variable x ...