[plt-scheme] Template bindings

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Jun 16 16:25:15 EDT 2002

While doing some macro stuff I stumbled on a small thing that didn't
take long to figure out but I think it's subtle enough to be worth the
time mentioning it somewhere...

The thing (which was a bit surprising when it was buried under a lot
of other junk) is that the lexical scope of both template variables
and normal variables is the same.  This seems pretty obvious when you
get an "identifier used out of context" error from

  (define-syntax (foo stx)
    (syntax-case stx () ((_ x) (let ((y 9)) #'y))))

or an "illegal use of syntax" from

  (define-syntax (foo stx)
    (syntax-case stx ()
      ((_ x) (let ((y 9)) (printf "-> ~s\n" x) #'1))))

but combining them got confusing -- with this macro:

  (define-syntax (foo stx)
    (syntax-case stx ()
      ((_ x) (let ((x 9)) (printf "-> ~s\n" (syntax-object->datum #'x)) #'1))))

I get "x" printed out no matter what argument `foo' gets which makes
sense in a non-trivial (IMO) way: the lexical binding shadows the
template binding, so now #'x treats the x as a normal identifier since
it doesn't see the previous template binding.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.