Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elixir will warn when variables prefixed with underscore are used #18

Open
eksperimental opened this issue Apr 3, 2021 · 3 comments
Open

Comments

@eksperimental
Copy link
Contributor

underscore_variable() ->
  case 1 of
    x when is_atom(x) ->
      atom;
    _Other ->
      _Other
  end.

will transpile as

  defp underscore_variable() do
    case (1) do
      :x when is_atom(:x) ->
        :atom
      _Other ->
        _Other
    end
  end

but the compiler will emit a warning:

warning: the underscored variable "_Other" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore
  out/samples/cornercases.ex:215: :cornercases.underscore_variable/0

I think the underscore should be removed

@marianoguerra
Copy link
Owner

The problem is that there may be a variable called Other, removing the underscore may change the meaning of the program.

@eksperimental
Copy link
Contributor Author

eksperimental commented Apr 6, 2021

A good compromise would be to remove the underscore if there is no other variable named like that.
If there is, leave it and let the user deal with the warning.
Having _Other and Other and using both is bad practice anyway.

@eksperimental
Copy link
Contributor Author

eksperimental commented Apr 6, 2021

I see what you mean. It is related to the other variable scope issue. It could use the name of another variable defined outside case, or the program relies on the variable after case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants