Subject: Re: [erlang-questions] Should erlang have macro
FUNCTION_ARGS ?




> On 7/08/2017, at 1:41 PM, Zhongzheng Liu <[email protected]> wrote:
>
> Hi mail-list:
>
> We have FUNCTION_NAME and FUNCTION_ARITY in OTP 19+.
>
> Is there FUNCTION_ARGS to get the argument list in current functions?

What is "THE argument list"?

Suppose I have
foo(X, [a,b,c], 42) -> ...
foo(0, Y, 127) -> ...

which clause defines *THE* argument list?
>
> Using this macro, we can use erlang:apply(fun
> ?FUNCTION_NAME/?FUNCTION_ARITY, ?FUNCTION_ARGS) to make any function
> recursively.

Unfortunately, the only kind of recursion you can create
this way is the kind you don't want. Since any variables
there might happen to be in the arguments cannot have changed,
the new call has *exactly* the same arguments as the old, and
will do *exactly* the same as the original call, including the
recursive call.

> After preprocessing, the example will look like:
>
> b_function(A) ->
> do_something(),
> erlang:apply(fun b_function/1, [A]).

This is a non-terminating function, for example.

>
> 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?

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.


_______________________________________________
erlang-questions mailing list
[email protected]
http://erlang.org/mailman/listinfo/erlang-questions



Programming list archiving by: Enterprise Git Hosting