I’m reading a book (Programming Elixir) where it’s stated that the following code:
line_no = 50
if (line_no == 50) do
IO.puts "new page\f"
line_no = 0
gives the following warning:
warning: the variable "line_no" is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. ...
I tried that code but I don’t have any warnings.
But since I tried with latest elixir version, I thought that it might be a warning in previous versions of elixir.
So I tried to search on Github some literals of the message (for example is unsafe or has been set inside or explicitly return) but I cannot find any result.
If You try this code in the console You will have an error
iex(1)> line_no = 50
iex(2)> if line_no == 50 do
...(2)> IO.puts "new page\f"
...(2)> line_no = 0
warning: variable "line_no" is unused (if the variable is not meant to be used, prefix it with an underscore)
Which version of Elixir do You use?
It is also different from Ruby code, mainly because of immutability. But here the problem is with scope. It should be…
I probably didn’t explain very well…
But I’m not talking about “unused” variable which is actually displayed as a warning. But about unsafe variable…
If you ran that code with the latest version of elixir as me, it also probably didn’t show up…
I don’t know why the search don’t include all the branches or how to search on a particular branch only because I chose the v1.0 branch and did the search there but according to the URL of the search it was only for master, anyway…)
Then I looked on the current file here:
So it seems that this warning is still here and should apply…
Yeah, I only noticed that now… It would have been nice if search was scoped to branches and also in the history (in older commits).
Though, for the later, since search results also displays commits, we can still found on history by checking one at a time on the history commits.
Ok that makes sense!
So this is not applicable anymore.
The book I’m reading cover elixir from 1.6 hence the error at this moment.
Anyway, in the book they described the reason behind this warning because of the high probability “to forget to initialize the variable outside the block, but to then rely on it having a value after the block”.
And I think that if this was the only reason, it made sense and it makes sense to still have the warning imho…