[erlang-patches] Epp Bugfix and macros overloading

Robert Virding rvirding@REDACTED
Fri Oct 16 00:55:30 CEST 2009


2009/10/9 christopher faulet <christopher.faulet@REDACTED>

> Hi all,
>
> Here are two patches for the module epp in R13B02-1.
>
> * The second patch includes the first one and adds support of multiple
> definitions for macros in the module epp (i.e. with the same name but
> with different arities). This feature wouldn't break any code (I hope so
> :) and might be usefull.
>
> Here is an simple example that uses it:
>
> =============================
> -module(test).
>
> -export([test/0]).
>
> -define(MY_MACRO(),    foo).
> -define(MY_MACRO,      bar).
> -define(MY_MACRO(X,Y), {X, Y}).
>
>
> test() ->  %% return [bar, foo, foo, {foo, bar}]
>    [
>     ?MY_MACRO,          %% use the 2nd def, replaced by bar
>     ?MY_MACRO(),        %% use the 1st def, replaced by foo
>     ?MY_MACRO(foo),     %% use the 2nd def, replaced by bar(foo)
>     ?MY_MACRO(foo, bar) %% use the 3rd def, replaced by {foo,bar}
>    ].
>
> bar(X) ->
>    X.
> =============================
>

I rather like this extension, it is something I always meant to do but never
got around to doing.

I am not sure, though, that I completely agree with how you choose which
definition to use, especially the third case here which I think should give
an undefined macro error. My reasoning is that if you define a macro to have
arguments, one or many definitions, and call it with arguments, even with an
empty argument list then you should only try for the matching definition and
not take the one without arguments. So if:

-define(M, a).
-define(M(), b).
-define(M(X,Y), {X,Y}).

then

?M        - should use 1st def
?M()     - should use 2nd def
?M(a,b) - should use 3rd def
?M(a)   - should generate an error

However if only:

-define(M, a).

then all calls will use this definition.

Robert


More information about the erlang-questions mailing list