Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Elixir variables really immutable?

In Dave Thomas's book Programming Elixir he states "Elixir enforces immutable data" and goes on to say:

In Elixir, once a variable references a list such as [1,2,3], you know it will always reference those same values (until you rebind the variable).

This sounds like "it won't ever change unless you change it" so I'm confused as to what the difference between mutability and rebinding is. An example highlighting the differences would be really helpful.

like image 638
Odhran Roche Avatar asked Apr 30 '15 11:04

Odhran Roche


1 Answers

Don't think of "variables" in Elixir as variables in imperative languages, "spaces for values". Rather look at them as "labels for values".

Maybe you would better understand it when you look at how variables ("labels") work in Erlang. Whenever you bind a "label" to a value, it remains bound to it forever (scope rules apply here of course).

In Erlang you cannot write this:

v = 1,      % value "1" is now "labelled" "v"             % wherever you write "1", you can write "v" and vice versa             % the "label" and its value are interchangeable  v = v+1,    % you can not change the label (rebind it) v = v*10,   % you can not change the label (rebind it) 

instead you must write this:

v1 = 1,       % value "1" is now labelled "v1" v2 = v1+1,    % value "2" is now labelled "v2" v3 = v2*10,   % value "20" is now labelled "v3" 

As you can see this is very inconvenient, mainly for code refactoring. If you want to insert a new line after the first line, you would have to renumber all the v* or write something like "v1a = ..."

So in Elixir you can rebind variables (change the meaning of the "label"), mainly for your convenience:

v = 1       # value "1" is now labelled "v" v = v+1     # label "v" is changed: now "2" is labelled "v" v = v*10    # value "20" is now labelled "v" 

Summary: In imperative languages, variables are like named suitcases: you have a suitcase named "v". At first you put sandwich in it. Than you put an apple in it (the sandwich is lost and perhaps eaten by the garbage collector). In Erlang and Elixir, the variable is not a place to put something in. It's just a name/label for a value. In Elixir you can change a meaning of the label. In Erlang you cannot. That's the reason why it doesn't make sense to "allocate memory for a variable" in either Erlang or Elixir, because variables do not occupy space. Values do. Now perhaps you see the difference clearly.

If you want to dig deeper:

1) Look at how "unbound" and "bound" variables work in Prolog. This is the source of this maybe slightly strange Erlang concept of "variables which do not vary".

2) Note that "=" in Erlang really is not an assignment operator, it's just a match operator! When matching an unbound variable with a value, you bind the variable to that value. Matching a bound variable is just like matching a value it's bound to. So this will yield a match error:

v = 1, v = 2,   % in fact this is matching: 1 = 2 

3) It's not the case in Elixir. So in Elixir there must be a special syntax to force matching:

v = 1 v = 2   # rebinding variable to 2 ^v = 3  # matching: 2 = 3 -> error 
like image 135
Miroslav Prymek Avatar answered Oct 18 '22 09:10

Miroslav Prymek