Module iif
Authors: Serge Aleynikov (saleyn(at)gmail(dot)com).
Description
Conditional expression functions
This module exports a parse transform and implements several condition-checking functions.
When using this as a parse transform, include the{parse_transform,iif}
compiler option. In this case for given expressions A
,B
,C
, and D
the following code transforms will be done:
iif(A, B, C) -> begin V = A, case V of true -> B; _ -> C end end
iif(A,B,C,D) -> case A of B -> C; _ -> D end
nvl(A,B) -> case A of false -> B; undefined -> B; [] -> B; _ -> A end
nvl(A,B,C) -> case A of false -> B; undefined -> B; [] -> B; _ -> C end
For debugging the AST of the resulting transform, use iif_debug
command-line option:
erlc -Diif_debug=1 ... % Prints AST before the transform
erlc -Diif_debug=2 ... % Prints AST after the transform
erlc -Diif_debug[=3] ... % Prints AST before/after the transform
Alternative to using this module as a parse_transform, it implements
several iif/3,4
exported functions that can be used without the transform.
Function Index
format_ne/3 | Format if first argument is not empty. |
ife/2 | Return Value if first argument is one of: [] , false , undefined . |
ife/3 | Return Empty if first argument is one of: [] , false , undefined . |
ifne/2 | Return Value if first argument is not one of: [] , false , undefined . |
ifne/3 | Return NotEmpty if first argument is not one of: [] , false , undefined . |
iif/3 | Return True if first argument is true or return False if
the first argument is one of: [] , false , undefined . |
iif/4 | Return True if first two arguments match. |
nvl/2 | Alias for ife/2 |
parse_transform/2 | Parse transform to be used by providing {parse_transform, iif} option. |
Function Details
format_ne/3
format_ne(True, Fmt, Args) -> any()
Format if first argument is not empty
ife/2
ife(Test, Value) -> any()
Return Value
if first argument is one of: []
, false
, undefined
.
Otherwise return the value of the first argument.
ife/3
ife(Value, Empty, NotEmpty) -> any()
Return Empty
if first argument is one of: []
, false
, undefined
.
Otherwise, if NotEmpty
is fun()
, evaluate it, or if it's fun(Arg)
evaluate it with Value
argument.
ifne/2
ifne(Test, Value) -> any()
Return Value
if first argument is not one of: []
, false
, undefined
.
Otherwise, if Value
is fun()
, evaluate it, or if it's fun(Arg)
evaluate it with Test
argument.
If not empty
ifne/3
ifne(Value, NotEmpty, Empty) -> any()
Return NotEmpty
if first argument is not one of: []
, false
, undefined
.
Otherwise, if NotEmpty
is fun()
, evaluate it, or if it's fun(Arg)
evaluate it with Value
argument.
iif/3
iif(X1, True, False) -> any()
Return True
if first argument is true
or return False
if
the first argument is one of: []
, false
, undefined
.
iif/4
iif(Value, Other, True, False) -> any()
Return True
if first two arguments match
nvl/2
nvl(Value, IfNull) -> any()
Alias for ife/2
parse_transform/2
parse_transform(AST, Opts) -> any()
Parse transform to be used by providing {parse_transform, iif}
option.
Opts
are compiler options passed from command line. E.g.:
erlc -Diif_ast ... -> Opts = [{d,iif_ast}|_]
erlc -Diif_orig ... -> Opts = [{d,iif_orig}|_]
erlc -Diif_src ... -> Opts = [{d,iif_src}|_]