Module defarg
Authors: Serge Aleynikov (saleyn(at)gmail(dot)com).
Description
Erlang parse transform for permitting default arguments in functions
Presently the Erlang syntax doesn't allow function arguments to have default parameters. Consequently a developer needs to replicate the function definition multiple times passing constant defaults to some parameters of functions.
This parse transform addresses this shortcoming by extending the syntax
of function definitions at the top level in a module to have a default
expression such that for A / Default
argument the Default
will be
used if the function is called in code without that argument.
-export([t/2]).
test(A / 10, B / 20) ->
A + B.
The code above is transformed to:
-export([t/2]).
-export([t/0, t/1]).
test() -> test(10);
test(A) -> test(A, 20);
test(A,B) -> A+B.
The arguments with default values must be at the end of the argument list:
test(A, B, C / 1) -> %% This is valid
...
test(A / 1, B, C) -> %% This is invalid
...
Default arguments must be constants or arithmetic expressions. Function
calls are not supported as default arguments due to the limitations of the
Erlang parser.
Function Index
parse_transform/2 | parse_transform entry point. |
Function Details
parse_transform/2
parse_transform(AST, Options) -> any()
parse_transform entry point