- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 87字
- 2021-08-05 10:42:49
if and unless
These two constructs can be used with the following syntax:
if <expression> do
# expression was truthy
else
# expression was falsy
end
unless <expression> do
# expression was falsy
else
# expression was truthy
end
As with the def construct, they can be inlined. For if, you'd do this:
if <expression>, do: # expression was truthy, else: # expression was falsy
For both constructs, the else clause is optional. They will return nil if the main clause doesn't match and no else clause was provided.