[erlang-questions] Should erlang have macro FUNCTION_ARGS ?

Zhongzheng Liu <>
Mon Aug 7 10:22:53 CEST 2017


Hi Richard:

> What is "THE argument list"?
> Suppose I have
>   foo(X, [a,b,c], 42) -> ...
>   foo(0, Y, 127) -> ...
> which clause defines *THE* argument list?

The argument list will change its value in different clause, just like ?LINE.

> >
> > Using this macro, we can use io:format("~p", [FUNCTION_ARGS]) to print
> > arguments in any function.
> True.  Considering that arguments can be extremely large,
> just how often do we want to print all the arguments unselectively?

Using io:format/2 to print arguments is just a example to explain the usage.
In practice I can send arguments to tx(https://github.com/kvakvs/tx)
or store them in somewhere for further analysis.

> I don't think this *CAN* work in the presence of maps.
> In a pattern (like the argument list), you have to write
> #{ key1 := val1, ..., keyn := valn }
> but that syntax is not allowed as an expression.  You have
> to write
> #{ key1 => val1, ..., keyn => valn }
> for that.  For example:
>     d_function(#{ a => X }) ->    % syntax error here
>        e_function(#{a => X});     % ok
>     d_function(#{ b := Y }) ->    % ok
>        e_function(#{b := Y}).     % syntax error here
> I am not thrilled about this discrepancy.  No other language
> I know does this, and it means that I can define a macro
> -define(MAGIC_VALUE, ...any ground term...).
> that stands for a ground term, and I can use that macro
> freely in patterns AND expressions,
>   EXCEPT IF IT CONTAINS A MAP.
> But that is the way it is, like it or not.

I think it can work with temporary variable.

     foo(X, [a,b,c], 42) ->
            ?FUNCTION_ARGS.

will become:

     foo(X=__TEMP_1__,  [a,b,c]=__TEMP_2__,  #{ b := Y }=__TEMP_3__) ->
            [__TEMP_1__, __TEMP_2__, __TEMP_3__] .

It will work with maps.


More information about the erlang-questions mailing list