There is always the option to refactor:
def eat_with_forks (phil, table) do
receive do
{:eat, forks} ->
phil
|> eat(forks, table)
|> think(table)
end
end
def dine(phil, table) do
send table, {:sit_down, self(), phil}
phil
|> eat_with_forks(table)
|> dine(table)
end
In other case of if statement, I am annoyed.
There is no equivalent to C’s:
if '(' expr ')' stmt [ else stmt ]
The if
in Elixir is like the ternary or conditional operator:
logical-OR-expression ?
expression :
conditional-expression
so there always has to be an else
- if you leave out the else
it assumes:
logical-OR-expression ?
expression : nil
which may not be what you want.
Furthermore between the if
and the else
and the else
and the end
isn’t a block of statements - it’s a sequence of expressions; the value of that sequence is the value of the last expression in the sequence - the values of the preceding expressions are ignored which means they are executed for their side-effect (which often is binding the value to a name).
Finally =
isn’t an assignment - it’s a match - the name(s) on the left hand side is(are) bound to a value(values) that will make it(them) match to the right hand side (if that is possible - see the pin operator).
Also see: