toraritte
toraritte

Reputation: 8243

How to write a multiline `do` block with the `do:` atom syntax?

For example, how would this module definition look like on one line?

defmodule Lofa do
  def miez do
    IO.puts("hello")
    a = 27
    IO.puts("bye")
    a
  end
end

Upvotes: 0

Views: 264

Answers (1)

toraritte
toraritte

Reputation: 8243

Just realized that one simply uses parentheses ((, )) around the block and semicolons (;) to separate the expressions:

defmodule Lofa, do: (def miez, do: (IO.puts("hello");a = 27;IO.puts("bye");a))

or taking it further:

iex(75)> defmodule(Lofa, [{:do, def(miez, [{:do, (IO.puts("hello"); a = 27; IO.puts("bye"); a)}])}])

Questions

The parentheses do not provide a lexical scope, it seems to be just for grouping:

iex(1)> a = :lofa                                                        
:lofa

iex(2)> (&(&1)).( (IO.puts("hello"); a = 27; IO.puts("bye"); a) )          
hello
bye
27

iex(3)> a
27

Related

Upvotes: 2

Related Questions