[gambit-list] Syntax-case, define-structure, and internal defines...

TJ tjay.dreaming at gmail.com
Mon Apr 16 13:20:06 EDT 2007


I have a macro defined thus:

(define-syntax define-only
  (lambda (exp)
    (syntax-case exp ()
      ((_ (n ...) e ...)
       (with-syntax (((tmp ...)
                      (generate-temporaries (syntax (n ...)))))
         (syntax
          (begin (define n #f) ...
                 (let ((tmp #f) ...)
                   ((lambda ()
                      e ...
                      (set! tmp n) ...))
                   (set! n tmp) ...))))))))

Which, if it works, would enable me to do the following:

(define-only (mkcounter inc! val)
    (define-structure counter v) ;; Invisible from the top-level.
    (define mkcounter
      (lambda () (make-counter 0)))
    (define inc!
      (lambda (c)
        (counter-v-set! c
                        (+ 1 (counter-v c)))))
    (define val counter-v))

But which, unfortunately, gives me the following:

*** ERROR -- invalid context for definition (define mkcounter (lambda
() (make-counter 0)))

I suspected that perhaps (define...)'s were not allowed after
(define-structure...)'s. So I did this:

(pp (lambda () (define-structure test a) 'done))

Which prints the following:

(lambda ()
  (letrec ((##type-1-test
            (##structure
             ##type-type
             (##make-uninterned-symbol "##type-1-test")
             'test
             8
             #f
             '#(a 0 #f)))
           (make-test (lambda (p1) (##structure ##type-1-test p1)))
           (test? (lambda (obj) (##structure-direct-instance-of? obj
(##type-id ##type-1-test))))
           (test-a (lambda (obj) (##direct-structure-ref obj 1
##type-1-test test-a)))
           (test-a-set!
            (lambda (obj val) (##direct-structure-set! obj val 1
##type-1-test test-a-set!))))
    'done))

Which makes me wonder why my macro doesn't work. After all
(define...)'s are allowed at the beginning of a letrec's body, no?

TJ



More information about the Gambit-list mailing list