[erlang-questions] scope of variables

Richard Carlsson richardc@REDACTED
Mon Sep 4 13:15:09 CEST 2006


Vlad Dumitrescu wrote:
> On 9/4/06, Richard Carlsson <richardc@REDACTED> 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).

So, how is that not hygienic?

> Oh, so even anonymous funs can be inlined? That's interesting!

Yes, the inliner does not really see a difference between normal
functions and anonymous funs. In both cases, it's just a functional
value bound to a name, and both can be inlined equally well.
The Core Erlang code produced for the function foo() above,
when using -compile(inline), is simply:

'foo'/0 = fun () -> 4

(This is Core Erlang syntax for "foo() -> 4" - it does not
mean that a fun-object will be created.)

	/Richard




More information about the erlang-questions mailing list