iif (etran v0.5.3)

View Source

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)      -> case A of true -> B; _ -> undefined end
   iif(A, B, C)   -> case A of true -> B; _ -> C 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.

Summary

Functions

Format if first argument is not empty

Return Value if first argument is one of: [], false, undefined. Otherwise return the value of the first argument.

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.

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

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.

Return True if first argument is true or return False if the first argument is one of: [], false, undefined.

Return True if first two arguments match

Parse transform to be used by providing {parse_transform, iif} option. Opts are compiler options passed from command line. E.g.

Functions

format_ne(True, Fmt, Args)

Format if first argument is not empty

ife(Test, Value)

Return Value if first argument is one of: [], false, undefined. Otherwise return the value of the first argument.

ife(Value, Empty, NotEmpty)

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(Test, Value)

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(Value, NotEmpty, Empty)

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(_, True, False)

Return True if first argument is true or return False if the first argument is one of: [], false, undefined.

iif(Value, Other, True, False)

Return True if first two arguments match

nvl(Value, IfNull)

Alias for ife/2

parse_transform(AST, Opts)

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}|_]