Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Bring shadowed variables warnings in functions back #84
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
Sorry for a slight off-topic, but there can be another issue here.
x = 1 y = fn(x) -> x = 2 end y.(1) #=> 2 y.(2) #=> 2 y.(3) #=> 2
This is a side-effect of the ability to reassign a variable, however is it a desired one? It's a perfectly correct piece of code, but it can be unclear at first what is going on. Maybe we could benefit from another warning about reassigning function params?
A good place to start is here:
Notice we simply consider a function clause to be a normal assignment, which, by Elixir standards, will reassign a new variable. So when you do:
Elixir transforms it to:
We need a way to tell Elixir to not generate a new variable... it could simply reuse the previous one. This case is handled by this clause:
Basically it says that, if we have already a version of
A good question here is whether we should change this. May be it is good
On Fri, Nov 2, 2012 at 5:25 PM, José Valim firstname.lastname@example.org wrote:
Maybe we are fine with the current behavior. I believe Erlang warns you because you may think it is a match. For example:
You could mistakenly think that you are matching by A in the function head (and therefore the function would only work when A is equal to one) but that is not actually what is happening. So it is a way of erlang to let you know that you are shadowing a variable.
In our case, since the default is to re-assign the variable anyway, we could argue that it is actually ok. In fact, we could trigger the erlang warning today simply by adding
However, other languages have semantics similar to Elixir but they still warn you.
I like that you can do
fn(^a) -> a + 1 end
except that it dodnt work for me :)
iex(1)> a=1 1 iex(2)> fn(^a) -> a + 1 end #Fun<erl_eval.6.82930912>
On Fri, Nov 2, 2012 at 5:58 PM, José Valim email@example.com wrote:
So, what should we do, close the issue or enable the warning?
On Fri, Nov 2, 2012 at 6:02 PM, José Valim firstname.lastname@example.org wrote: