Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: rationale behind allowing rebinding of variables

Tags:

erlang

elixir

What is the rationale behind allowing rebinding of variables in Elixir, when Erlang doesn't allow that?

like image 467
tldr Avatar asked Feb 09 '16 23:02

tldr


2 Answers

Most functional languages don't allow rebinding of variables in the same scope. So Elixir allowing this does definitely give it an non-functional, imperative feel. Erlang's problem is rather lack of scope, or to be more precise that there is only one scope in a whole function clause. We did have serious discussions whether to introduce scope but in the end we decided against it as it was backwards incompatible with the existing system. And developers hate backwards inconsistent changes.

The Erlang way has one serious benefit: when you get it wrong you generally get an error so you can SEE the mistake. This compared just getting strange behaviour when a variable doesn't have the value you expect it to have which is MUCH harder to detect and correct.

Personally I think that the problem of new variable names, for example using the number scheme, is greatly overblown. Compared to the time it takes me to work out WHAT I am going to do changing variable names is trivial. And after a while you just see it without reflecting about it. Honestly.

EDIT:

Also when chaining data through a sequence of functions the actual meaning of the data changes so reusing the same variable name can be very misleading. It can end up just meaning a generic "data I am passing from one function to another".

like image 130
rvirding Avatar answered Nov 15 '22 14:11

rvirding


Here's the rationale straight from the horse's mouth:

http://blog.plataformatec.com.br/2016/01/comparing-elixir-and-erlang-variables/

like image 10
asonge Avatar answered Nov 15 '22 14:11

asonge