3
votes

How do I code multiple statements in a single Elixir receive do pattern match?

This works:

def pong sender do
  receive do
    x -> IO.puts("hello"); IO.puts("there"); send(sender, x)
  end
end

But what if I cannot put them all on the same line? Can they be bracketed using a do end clause? Because this does not work:

def pong sender do
  receive do
    x -> do
           IO.puts("hello")
           IO.puts("there")
           send(sender, x)
         end
  end
end
1
You can remove the do and end from your second example and it will work. Like you can do with case elixir-lang.org/getting-started/case-cond-and-if.html#case - Gazler

1 Answers

12
votes

I think you can just omit do/end:

def pong sender do
  receive do
    x ->
      IO.puts("hello")
      IO.puts("there")
      send(sender, x)
  end
end