[plt-scheme] Macro-expansion-time structs

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Oct 12 07:11:02 EDT 2008

At Sun, 12 Oct 2008 10:06:22 +0200, "Jos Koot" wrote:
> Would (begin-for-syntax (define-struct etc ())) not work as well?

The problem with

 #lang scheme/base
 (require (for-syntax scheme/base))
 (begin-for-syntax 
  (define-struct etc ()))

is that `define-struct' expands to both `define' and `define-syntax',
and `define-syntax' forms are not currently supported within
`begin-for-syntax' (i.e., there's not `define-syntax-for-syntax').


You can write

 #lang scheme/base
 (require (for-syntax scheme/base))

 (begin-for-syntax 
  (define-struct etc () #:omit-define-syntaxes))

which doesn't define `etc', so the structure type doesn't work with
`match' or other forms that need expand-time information about the
structure type. But it does define `make-etc', `etc?', and so on, which
is enough for many purposes.


Matthew


> ----- Original Message ----- 
> From: "Carl Eastlund" <cce at ccs.neu.edu>
> To: "Henk Boom" <lunarc.lists at gmail.com>
> Cc: "Eli Barzilay" <eli at barzilay.org>; <plt-scheme at list.cs.brown.edu>
> Sent: Sunday, October 12, 2008 6:37 AM
> Subject: Re: [plt-scheme] Macro-expansion-time structs
> 
> 
> > If you want the my-box struct bound at the module level, you can
> > create another module, use define-struct normally, then import it for
> > syntax.  Or you can use define-values-for-syntax like this (which
> > won't get the struct's syntax binding, but will get anything else you
> > need):
> >
> > #lang scheme/base
> >
> > (require (for-syntax scheme/base))
> >
> > (provide test)
> >
> > (define-values-for-syntax (make-my-box my-box-val my-box?)
> > (let ()
> >   (define-struct my-box (val))
> >   (values make-my-box my-box-val my-box?)))
> >
> > (define-syntax test
> >  (lambda (stx)
> >    #`#,(my-box-val (make-my-box 2))))
> >
> > On Sat, Oct 11, 2008 at 11:22 PM, Henk Boom <lunarc.lists at gmail.com> 
> > wrote:
> >> 2008/10/11 Carl Eastlund <cce at ccs.neu.edu>:
> >>> You've defined the struct in the runtime environment.  You need to
> >>> define it in the transformer environment if you want transformers to
> >>> use it.  You might accomplish that like this (though this makes a
> >>> local definition other transformers won't be able to use):
> >>>
> >>> #lang scheme/base
> >>>
> >>> (require (for-syntax scheme/base))
> >>>
> >>> (provide test)
> >>>
> >>> (define-syntax test
> >>>  (let ()
> >>>   (define-struct my-box (val))
> >>>   (lambda (stx)
> >>>     #`#,(my-box-val (make-my-box 2)))))
> >>
> >> I see. It's a pity that (begin-for-syntax (define-struct . . .)) doesn't 
> >> work.
> >>
> >> Thanks for the info!
> >>    Henk
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > 
> 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.