[erlang-questions] scope of variables

Vlad Dumitrescu <>
Mon Sep 4 13:36:00 CEST 2006


On 9/4/06, Richard Carlsson <> wrote:
> Vlad Dumitrescu wrote:
> > On 9/4/06, Richard Carlsson <> wrote:
> >> and perhaps define macros that use such lambdas:
> >> -define(let(X, Y, Z), ((fun (X)-> Z end)(Y))).
> >
> > Is it really that simple? This doesn't make for hygienic macros, and
> > this will fail with a badmatch:
> >
> > foo() ->
> >    A = 2,
> >    ?let(X, 3, begin A=1, X+A end).
>
> Yes, but that is not so strange. The macro does not create a completely
> blank scope (it would be pretty useless if you could not import any
> existing bindings). If you rewrite your example like this, there
> is no problem:
>
> foo() ->
>    A = 2,
>    ?LET(A, 1, ?LET(X, 3, X+A)).
>
> (which returns 4, not 5).

True, in this case it is easy to rewrite it, but it might be much more
difficult in the general setting, where the variable A isn't top-level

foo() ->
   A = 2,
   ?let(X, [3, 3], begin case X of [A|B] -> B+A; _ -> -1 end).

This is simple to rephrase, but imagine there are 10-12 of those free
variables. It's not worth rewriting everything just to be able to use
?let, right? It can be done, but it's work that the compiler should
do, not the programmer, for a language to be able to claim it has
proper macros.

Anyway, scoping is just _one_ of the issues involved... *sigh*

best regards,
Vlad



More information about the erlang-questions mailing list