[racket] Binding undefined with a macro

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Tue Sep 3 12:36:33 EDT 2013

The struct form generates names unhygienically, but predictably. You will have to have a handle on both the struct name and field name to produce an identifier equal to the generated field accessor.
In your case, if you can commit to name always being given first (there are ways around this that involve more parsing), then the following will allow you to write your macro correctly:

(require (for-syntax racket/syntax))
(define-syntax (struct-abc stx)
   (syntax-case stx ()
     [(_ sname (name args ...) props ...)
      (with-syntax ([sname-name (format-id #'sname "~a-~a" #'sname #'name)])
      (+ 1 1)
      (struct sname (name args ...) props ...
          #:property prop:insiders sname-name)))]))

----- Original Message -----
From: "antoine" <antoine597 at gmail.com>
To: users at racket-lang.org
Sent: Tuesday, September 3, 2013 12:23:17 PM GMT -05:00 US/Canada Eastern
Subject: [racket] Binding undefined with a macro


With this macro:

(define-syntax (struct-abc stx)
   (syntax-case stx ()
     [(_ name (args ...) props ...)
      (+ 1 1)
      (struct name (args ...) props ...
          #:property prop:insiders person-name))]))

(define-values (prop:insiders insiders? insiders-ref)
   (make-struct-type-property 'insider))

When i do :

(struct-abc person (name nickname))

I get :

person-name: undefined;
  cannot reference an identifier before its definition

But i know that person-name is defined the problem come from 'begin' in 
the macro, if i rewrite like this :

(define-syntax (struct-abc stx)
   (syntax-case stx ()
     [(_ name (args ...) props ...)
      #`(struct name (args ...) props ...
          #:property prop:insiders person-name)]))

It works as expected.

So my question are:

Could you explain (point out) me why the person-name is unknown at this 
And how can i change the begin with something like '#,@'?

Thank you.
  Racket Users list:

Posted on the users mailing list.