[plt-scheme] macro differences in REPL vs. in a module?

From: Benderjg2 at aol.com (Benderjg2 at aol.com)
Date: Wed Sep 11 21:06:12 EDT 2002

In some code I've been working on today, I have seen something curious: 
macros which work when used in the REPL/"at the top level"-- but which given 
an error message when used with in a module.

The full example is rather long, but complains:
  compile: bad syntax; function application is not allowed, because no #%app 
syntax transformer is bound in: ...

In a rather stipped down version, I am still able to produce a (different) 
error when my stripped-down macro is used within a module vs. in the REPL 
(where the code works "fine"). This stripped down case is given below. When 
the define-struct/prop macro is used within a module, the error message given 
is:
  module: identifier originates in a different module in: acc

Note two (commented-out) lines within the with-syntax used in the definition 
of "define-struct/prop" which create identifiers for the accessor and mutator 
functions in the context of stx. When these are un-commented, this stripped 
down example seems to work fine in both settings.

What causes such differences, and perhaps more to the point- any tips as to a 
possible cause for the first error message above (about a function 
application not allowed)?

Thanks,
Jim

-----------------------------------------------
; the main module --or top-level code. this is tested in v202

(module try mzscheme
  
  (require "struct-prop.ss")
  
  (define-struct/prop test (a b c)
                      ((prop:writer write)))
  
  mut ; or mutator (when the syntax for acc and mut is uncommented)
  
  (provide struct:test make-test test?)
  
  )

-----------------------------------------------
; "struct-prop.ss"
#cs
(module struct-prop mzscheme
  
  (provide prop:writer
           writer?
           writer-ref
           define-struct/prop)
  
  (define-values (prop:writer writer? writer-ref) (make-struct-type-property 
'writer))
  
  (define-syntax (define-struct/prop stx)
    (syntax-case stx ()
      ((_ name (field ...) ((prop-name value) ...))
       (identifier? (syntax name))
       (with-syntax ((num-fields (length (syntax->list (syntax (field 
...)))))
                     (struct-desc 
                      (datum->syntax-object (syntax name)
                       (string->symbol 
                        (string-append "struct:" (symbol->string 
                                                  (syntax-object->datum 
(syntax name)))))
                       (syntax name) #f))
                     (cstr-name (datum->syntax-object (syntax name)
                       (string->symbol 
                        (string-append "make-" (symbol->string 
                                               (syntax-object->datum (syntax 
name)))))
                       (syntax name) #f))
                     (pred-name (datum->syntax-object (syntax name)
                       (string->symbol 
                        (string-append (symbol->string 
                                        (syntax-object->datum (syntax name))) 
"?"))
                       (syntax name) #f))
                     ; *** NOTE: with these two lines uncommented, this macro
                     ; *** behaves the same both within and outside a module
                     ;(acc (datum->syntax-object stx 'accessor))
                     ;(mut (datum->syntax-object stx 'mutator))
                     )
         (syntax 
          (define-values (struct-desc
                          cstr-name
                          pred-name
                          acc
                          mut)
            (make-struct-type 'name #f num-fields 0 #f (list (cons prop-name 
value) ...))))))))
  
  )



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20020911/4d152d0b/attachment.html>

Posted on the users mailing list.