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

From: Will M. Farr (wmfarr at gmail.com)
Date: Sun Nov 8 18:30:59 EST 2009

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)


(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?))))))

---------------------- test.ss:
#lang scheme

(require "define-pointer-type.ss"

(define-pointer-type _my-pointer my-pointer?)

  (_my-pointer ctype?))

Trying to require "test.ss" gives:

reference to an identifier before its definition: _my-pointer in  
module: "/private/tmp/test.ss"

The same thing happens if I use the value _my-pointer directly in any  
part of the test.ss module.  (For example, if I try to pull values out  
of a foreign library using a type like (_fun _my-pointer ...) things  
explode in the same way.)  It seems to me that this ought to work,  
though.  Is there something I'm missing, or is this a bug with the  
module system?


Posted on the users mailing list.