Skip to content

Instantly share code, notes, and snippets.

@tosik
Last active December 20, 2015 08:39
Show Gist options
  • Select an option

  • Save tosik/6101740 to your computer and use it in GitHub Desktop.

Select an option

Save tosik/6101740 to your computer and use it in GitHub Desktop.
why does elixir output the warning 'variable x is unused' ?
% elixir main.exs
> unused_x_variableain.exs:4: variable x is unused
> foo
defmodule Main do
def match(x) do
case :foo do
x -> 'foo'
_ -> 'bar'
end
end
end
IO.puts Main.match(:foo)
@mururu
Copy link

mururu commented Aug 1, 2013

twitterで見かけました。
elixirはパターンマッチで再束縛されちゃうので、それだとxは任意の値にマッチしてしまいます。
意図をつかみそこねてなければたぶん正しくはこうです。

defmodule Main do
  def match(x) do
    case :foo do
      ^x -> 'foo'
      _ -> 'bar'
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment