LoginSignup
9
0

Elixir case文にwhenが使えるって知ってた?

Last updated at Posted at 2023-12-15

case文って固定値との比較しかできないと思ってました。

マニュアルによると、Guard節がcase文で使えると書いてある。

image.png

defmodule Elixir101 do
  @doc """

  ## Examples

      iex> Elixir101.sample1(3)
      "two-ten"

  """
  def sample1(n) do
    case n do
      1 -> "one"
      _ when n <= 10 -> "two-ten"
      _ -> "over ten"
    end
  end
end

使えました。

参考
https://hexdocs.pm/elixir/1.16.0-rc.0/patterns-and-guards.html#guards

9
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
0