DRYな備忘録

Don't Repeat Yourself.

【Elixir】the result of the expression is ignored

問題

ElixirでFizzBuzz書いてて、掲題のように怒られる

main.exs:4: warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)

the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)

ソースコード

defmodule FizzBuzz do
  def to(max) do
    1..max |> Enum.each fn(n) ->
      case {rem(n, 3) == 0, rem(n, 5) == 0} do
        _ ->
          IO.puts n
      end
    end
  end
end

調査

Well, the compiler is telling you exactly what's wrong :) You create a new #tab_info record, but never bind it to any variable.

せやな感ある。

原因

the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)

和訳

評価の結果値が無視されてます( _ 変数に評価値を代入することで、警告を抑止してください)

いやーしてる気がするんだけどなー。和訳が間違ってるのかな。とりあず評価値を使ってあげればいいんだろうな。

解決

defmodule FizzBuzz do
  def to(max) do
    1..max |> Enum.each fn(n) ->
      case {rem(n, 3) == 0, rem(n, 5) == 0} do
        {true, true} ->
          IO.puts "FizzBuzz"
        _ ->
          IO.puts n
      end
    end
  end
end

とりあえず警告は消えた

DRYな備忘録