[plt-scheme] create complex object @ compile time but made available @ run time?

From: YC (yinso.chen at gmail.com)
Date: Wed Jun 13 02:59:32 EDT 2007

Hi all -

is it possible to create an complex object during syntax expansion but
return it as the result of the expansion?  It is possible to do so for basic
types such as numbers.

(module add mzscheme
  (define (add a b) (+ a b))
  (provide (all-defined)))
(require add)
(require-for-syntax add)
(define-syntax (macro-add stx)
  (syntax-case stx ()
    ((_ a b)
     (with-syntax ((val (datum->syntax-object
                         (add (syntax-object->datum #'a)
                              (syntax-object->datum #'b)))))
(= (add 5 6) (macro-add 5 6)) ; => #t ; as both returns 11.

But this does not hold for complex object such as struct with the following
code - is this something wrong with the code or is it something else?

(module foo mzscheme
  (define-struct foo (vals))
  (define (create-foo vals)
    (make-foo vals))
  (provide (all-defined)))
(require foo)
(require-for-syntax foo)
(define-syntax (macro-foo stx)
  (syntax-case stx ()
    ((_ val1 val2 ...)
     (with-syntax ((foo (datum->syntax-object
                         (create-foo (syntax->list #'(val1 val2 ...))))))
(define f1 (macro-foo 1 2 3))
f1 ; => shows #<struct:foo>
(foo? f1) ; => #f
(foo-vals f1) ; => error: foo-vals: expects args of type <struct:foo>; given
instance of a different <struct:foo>

Any insights are appreciated, thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070612/ab6c77da/attachment.html>

Posted on the users mailing list.