Kernel.if
You're seeing just the macro
if
, go back to Kernel module for more information.
Provides an if/2
macro.
This macro expects the first argument to be a condition and the second argument to be a keyword list.
One-liner examples
if(foo, do: bar)
In the example above, bar
will be returned if foo
evaluates to
a truthy value (neither false
nor nil
). Otherwise, nil
will be
returned.
An else
option can be given to specify the opposite:
if(foo, do: bar, else: baz)
Blocks examples
It's also possible to pass a block to the if/2
macro. The first
example above would be translated to:
if foo do
bar
end
Note that do/end
become delimiters. The second example would
translate to:
if foo do
bar
else
baz
end
In order to compare more than two clauses, the cond/1
macro has to be used.