On Nov 11, 2008, at 9:16 AM, Abdulaziz Ghuloum wrote:
On Nov 11, 2008, at 4:12 AM, marcomaggi wrote:
So it seems that macros at 'expand' time have access
to all the bindings that will be available at 'run'
time in the region of a macro use.
There might be different regions at the macro use
with different bindings.
Maybe you don't immediately see how this could be,
but here's an example of a macro that receives a
"macro-use-stx" containing two different contexts
with two different bindings of "x". Using #'ctxt1
(which refers to the identifier "gimme-x" in the
last line of code) gives you "inner". If you use
#'ctxt2 as the argument to datum->syntax, then you
get "outer" because that refers to the "x" that is
in scope where the word "here" appears in the code.
(let ()
(define-syntax gimme-x
(lambda (macro-use-stx)
(syntax-case macro-use-stx ()
[(ctxt1 ctxt2) (datum->syntax #'ctxt1 'x)])))
(let ([x 'outer])
(let-syntax ([with-outer-ctxt
(syntax-rules ()
[(_ k) (k here)])])
(let ([x 'inner])
(with-outer-ctxt gimme-x)))))
It may be tricky if you're just learning macros,
but that's why you should probably stick to hygienic
macros. I, for example, understand the hygiene
issues and datum->syntax very well (I implemented
them), and that's why I rarely use datum->syntax.
It's tricky to get right and you'll end up just
confusing yourself. Just an advice, take it or
leave it.
Aziz,,,