It is possible to pattern match with an OR | in a case clause

For example

case x do
  {:ok, :first | :third} -> 
    "do something"
  {:ok, :second} ->
    "do something"
end

Do I really have to copy and paste my code from the :first match to the :third match?
Or is there a better way to do this?

You could do the following using guards:

case x do
  {:ok, y} when y in [:first, :third] -> 
    "do something"
  {:ok, :second} ->
    "do something else"
end
7 Likes