[plt-scheme] Problem with define-syntax and class*

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Tue Dec 6 11:19:59 EST 2005

Hello all,

I have a little problem with macros and classes. This is the first  
time I've used macros so please forgive me if I'm being an idiot. I  
have a test case below. The (define obj-b ...) statement is supposed  
to be macro expanded to be the same as the (define obj-a ...) statement:

> (require (lib "class.ss"))
> (define tag%
>   (class* object% ()
>     (init-field
>      [alpha 'alpha]
>      [x #f]
>      [y #f]
>      [z #f]
>      [body null])
>     (super-new)))
> (define-syntax make-tag
>   (syntax-rules ()
>     [(make-tag tag-class tag-attributes tag-body)
>      (new tag-class ,@(tag-attributes) [body tag-body])]))
> (define obj-a (new tag% [x 1] [y 2] [z 3] [body '(a b c)]))
> (display (get-field x obj-a)) (newline)
> (display (get-field y obj-a)) (newline)
> (display (get-field z obj-a)) (newline)
> (display (get-field body obj-a)) (newline)
> (define obj-b (make-tag tag% ([x 1] [y 2] [z 3]) '(a b c)))
> (display (get-field x obj-b)) (newline)
> (display (get-field y obj-b)) (newline)
> (display (get-field z obj-b)) (newline)
> (display (get-field body obj-b)) (newline)

It's a class representing a kind of XML-like object. The code  
compiles fine, and when run in DrScheme (PLT Scheme v299.400), I get:

> 1
> 2
> 3
> (a b c)
> [BUG] reference to undefined identifier: x

I presume this is something to do with scoping... obviously I'm  
missing something. Any suggestions?

Thanks in advance,

-- Dave

Posted on the users mailing list.