weblogging at the beach

« Happy Valentine's from Untyped | Main | Naming Your Wireless »

February 15, 2008

Requiring up and down syntax levels

If you do any macro programming in PLT Scheme you are sure to run into the dreaded “no #%app syntax transformer is bound” error message at some point. Though puzzling, the fix is actually quite simple in almost all cases. Assuming you're using 3.99, you either need to:

  1. (require (for-syntax scheme/base))
  2. (require (for-template scheme/base))

What the error means is that some syntax has expanded in a function application, but #%app, the PLT Scheme primitive that actually handles application, is not bound in the phase in which the syntax is being evaluated. Requiring for-syntax will bind #%app in the phase before the current evaluation phase, while requiring for-template will bind #%app in the phase after. In most cases you want for-syntax. However, if you are writing functions that return syntax that is then inserted into a program (such a function would be required for-syntax elsewhere) you must use the other form, to make sure the syntax has #%app available to it.

Posted by Noel at February 15, 2008 10:59 AM

Trackback Pings

TrackBack URL for this entry:
http://www.untyped.com/mt/mt-tb.cgi/133

Comments

Though neither seem to work for my favorite case.
(define-syntax define
  (let-syntax ((olddefine (syntax-rules () ((_ args ...) (define args ...)))))
    ((define args ...) (begin
                                (print 'hi)
                                (olddefine args ...)))))

Posted by: PJW at February 18, 2008 06:50 PM