View Source defarg (etran v0.5.2)

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.

Link to this section Summary

Functions

parse_transform entry point

Link to this section Functions

Link to this function

parse_transform(AST, Options)

View Source
parse_transform entry point