[erlang-questions] scope of variables

David Hopwood david.nospam.hopwood@REDACTED
Mon Sep 4 16:59:02 CEST 2006


Vlad Dumitrescu wrote:
> On 9/4/06, Richard Carlsson <richardc@REDACTED> wrote:
> 
>>Ok, so it's not just a question of being able to use local bindings -
>>i's also about being able to inject code (the macro body) into some
>>arbitrary context and avoid accidental name capture of variables in the
>>macro. But then the injected code (not the context) must be written with
>>this in mind, or you will have to do renaming when you inject it.
> 
> Yes, or better: have the compiler do it for me. Which is what I'm trying to do.
> 
> If it will work as advertised, there are a lot of cool things that
> will become posible to do. Like for example optimizations. If I have
> to do a lot of math:pow(X, 72) with different X, I could write a
> powN/2 macro that produces an optimized powN/1 for any particular N
> (72 in this case). That would be
>   Pow72 = fun(X) ->
>     X2 = X*X,
>     X4 = X2*X2,
>     X8 = X4*X4,
>     X16 = X8*X8,
>     X32 = X16*X16,
>     X64 = X32*X32,
>    X64+X8.

X64*X8.

I'm also strongly of the opinion that hygiene should be an inherent property
of a macro system, and not dependent on hacks like gensym or use of funky
variable names. (It is OK to be able to *explicitly* declare that a variable
should be captured; this is occasionally useful.)

-- 
David Hopwood <david.nospam.hopwood@REDACTED>





More information about the erlang-questions mailing list