[plt-scheme] Keyword argument macros
Dave Gurnell
d.j.gurnell at gmail.com
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 --------------
Skipped content of type multipart/mixed
More information about the plt-scheme
mailing list