[plt-scheme] ffi equivalent of array in struct

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Mon Apr 24 20:34:31 EDT 2006

> This seems repetitive, so maybe a macro will help.
> Here's one that I just cooked up (starting to learn about quasiquotation 
> now...):

[code cut]

Hi Jon,

Whoops!  I made an error in forgetting to require-for-syntax the 
foreign.ss library, since it's being used by build-int-stx-list.  I'm 
still trying to keep my macro-expansion phases straight...

I'm pretty sure I have to do a require-for-syntax to make things kosher. 
The corrected code should be:

(module n-bytes mzscheme
   (require (lib "foreign.ss"))
   (require-for-syntax (lib "foreign.ss"))

   (define 4bytes _int)
   (define 8bytes (make-cstruct-type (list 4bytes 4bytes)))

   (define-for-syntax (multiple-of-four? n)
     (and (number? n) (= 0 (remainder n 4))))

   (define-for-syntax (build-int-stx-list stx n)
     (let loop ([n n]
                [acc #'()])
         [(= n 0) acc]
          (loop (- n 4)
                (quasisyntax/loc stx
                  (_int . #,acc)))])))

   (provide n-bytes)
   (define-syntax (n-bytes stx)
     (syntax-case stx ()
       [(_ n)
          [(multiple-of-four? (syntax-e #'n))
           (quasisyntax/loc stx
              (list #,@(build-int-stx-list stx (syntax-e #'n)))))]
            "expected numeric literal divisible by four"

But that's odd, though: I wonder why my tests appeared to work, even when 
the reference to "_int" within build-int-stx-list should have broke!  I 
guess my test cases weren't tight enough.

My apologies!

Posted on the users mailing list.