'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
Solution 1:[1]
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
The 21. Optional syntax sheet chapter in the "Getting Started" guide is exactly about this topic.
What are module and function names exactly? (they seem to be more than atoms)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |