Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir receive message: how do I run multiple statements?

Tags:

elixir

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
like image 655
Thomas Browne Avatar asked Sep 19 '25 16:09

Thomas Browne


1 Answers

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
like image 59
Alex Avoiants Avatar answered Sep 21 '25 17:09

Alex Avoiants