[plt-scheme] Macro-expansion-time structs

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sat Oct 11 23:14:47 EDT 2008

On Sat, Oct 11, 2008 at 10:10 PM, Henk Boom <lunarc.lists at gmail.com> wrote:
> 2008/10/11 Eli Barzilay <eli at barzilay.org>:
>> The answer to both of these questions is that you should use
>>
>>  (require (for-syntax scheme/base))
>
> Thank you for the quick reply!
>
> This solves one of the problems. However, something like this still
> doesn't work:
>
> ---
> #lang scheme/base
>
> (require (for-syntax scheme/base))
>
> (provide test)
>
> (define-struct my-box (val))
>
> (define-syntax test
>  (lambda (stx)
>    #`#,(my-box-val (make-my-box 2))))
> ---
> henk at korhal ~ $ mzscheme -i -t test.ss
> Welcome to MzScheme v4.1 [3m], Copyright (c) 2004-2008 PLT Scheme Inc.
> test.ss:11:9: compile: unbound variable in module (in the transformer
> environment, which does not include the run-time definition) in:
> my-box-val
> ---
>
> define-struct really seems to be a runtime thing. . .
>
>    Henk

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

-- 
Carl Eastlund


Posted on the users mailing list.