[erlang-questions] scope of variables

Erik Reitsma (RY/ETM) erik.reitsma@REDACTED
Mon Sep 4 15:35:44 CEST 2006


> If you know that the macros will not be nested, the simplest 
> solution is to use strange variable names in the macro 
> bodies, for local variables that are bound in patterns, as in:
> 
> -define(foo(X), case X of [__@REDACTED|__@REDACTED] -> __@REDACTED@a; _ -> -1 end).

When I try to compile this, R10B says that
 
./t1.erl:10: variable '__@REDACTED' unsafe in 'case' (line 9)
./t1.erl:10: variable '__@REDACTED' unsafe in 'case' (line 9)

and rightly so. Changing it to 

-define(foo(X), case X of [__@REDACTED,__@REDACTED] -> __@REDACTED@a end).

gives only warnings, but you cannot use this macro twice in the same
scope:

-module(t1).

-export([test/0]).

-define(foo(X),
case X of [__@REDACTED,__@REDACTED] -> __@REDACTED@a end).

test() ->
    ?foo([1,2]),
    ?foo([2,3]).

Here t1:test() results in an error, case_clause.
So, strange variables will only help if you do not use the macro twice.
I think this will lead to errors that are hard to trace.

*Erik.




More information about the erlang-questions mailing list