Appearance
Optional syntax sheet
Source: Elixir guide, Optional syntax sheet.
Elixir lets you omit a few delimiters. Four rules explain all of them, and they keep the core of the language tiny.
Start from this:
elixir
if variable? do
Call.this()
else
Call.that()
endNow take the conveniences away, one at a time:
do-endblocks are keywords:if variable?, do: Call.this(), else: Call.that()- A keyword list as the last argument needs no square brackets.
- A keyword list is a list of two-element tuples:
[{:do, …}, {:else, …}] - Parentheses are optional on function calls.
Why it matters
if, def and defmodule are not special keywords with their own grammar. They are ordinary calls built from these four rules. So this:
elixir
defmodule Math do
def add(a, b) do
a + b
end
endis just:
elixir
defmodule(Math, [
{:do, def(add(a, b), [{:do, a + b}])}
])That's what lets you extend the language with the same constructs it is built from (see meta-programming).
You don't have to apply these rules by hand: mix format does. It always adds parentheses to calls unless configured otherwise.