[plt-scheme] Macros that expand to (define ...) and "reference to an identifier before its definition" errors

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sun Nov 8 19:05:47 EST 2009

Will M. Farr wrote:
> Hello all,
> 
> I'm puzzled by a "reference to an identifier before its definition" 
> error that I keep getting in some code I'm working on.  Here is a small 
> example that shows the same behavior:
> 
> ---------- define-pointer-type.ss:
> #lang scheme
> 
> (require scheme/foreign
>          (rename-in scheme (-> ->/c)))
> 
> (provide define-pointer-type)
> 
> (unsafe!)
> 
> (define-syntax define-pointer-type
>   (syntax-rules ()
>     ((define-pointer-type name pred?)
>      (define-values (name pred?)
>        (let* ((tag (gensym name))
>               (name (_cpointer tag))
>               (pred? (lambda (obj)
>                        (and (cpointer? obj)
>                             (cpointer-has-tag? obj tag)))))
>          (values name pred?))))))

You forgot to quote the name in the call to gensym:

   (let* ((tag (gensym 'name)) ___) ___)

Unfortunately, the macro stepper doesn't help, because this isn't an 
expansion-time error; it's a run-time error. The clue is that neither 
the macro stepper or Check Syntax trigger the error.

For that kind of error, the best debugging tip I can think of is to try 
manually expanding your macro once, either using 'expand-once' or the 
macro stepper. Then see if you get more precise error information that 
way. (In this case, you would.)

Ryan


Posted on the users mailing list.