[plt-scheme] Keyword argument macros

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Tue Jan 6 08:52:20 EST 2009

Casey Klein wrote:
> I'd like to write a macro that accepts keyword arguments, e.g.,
> (my-macro e1 #:kw e2 e3). syntax-case isn't much help in parsing the
> macro's input. Do I need to do this parsing myself, checking for
> duplicate/missing keywords, or is there some library solution I'm
> missing?

syntax-case does correctly match on keywords if you use them in your  
patterns.

For what it's worth, I have adopted an idiom I found in the definition  
of new define-struct:

(define-syntax (my-form stx)

   (define (do-arguments args-stx)
     (syntax-case args-stx ()
       [() (do-final)]
       [(#:arg1 val other ...)
        (begin
          ; do something with the argument and its value
          (do-arguments #'(other ...)))]))

   (define (do-final)
     ; produce some final syntax
     )

   (syntax-case stx ()
     [(_ arg ...)
      (do-arguments #'(arg ...)]))

I typically store a bunch of state as internal defines and use it in  
do-final.

Something unrelated but potentially useful... I wrote a "human- 
friendly" macro version of keyword-apply that I'm going to add to the  
next version of Unlib.plt. Code is attached below.

Hope this helps,

-- Dave



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090106/f19177b9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: keyword.ss
Type: application/octet-stream
Size: 2623 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20090106/f19177b9/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090106/f19177b9/attachment-0001.html>

Posted on the users mailing list.