[plt-scheme] Keyword argument macros

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Tue Jan 6 13:00:36 EST 2009

On 6 Jan 2009, at 15:55, Casey Klein wrote:

> On Tue, Jan 6, 2009 at 7:33 AM, Noel Welsh <noelwelsh at gmail.com>  
> wrote:
>> Something that might work, depending on your macro
>>
>> Pass the arguments to the a define-for-syntax function with
>> appropriate keyword arguments.
>
> using keyword-apply?


Yes. That will work, but you still need to fold over the arguments to  
accumulate keyword/value pairs.

That keyword-apply* macro will do that for you. I've added it to Unlib  
(version 3.11):

#lang scheme

(require (for-syntax (planet untyped/unlib:3:11/keyword)))

(define-for-syntax keywords->alist
   (lambda (#:a [a 1] #:b [b 2] [c 3] [d 4]. rest)
     (list (cons 'a a)
           (cons 'b b)
           (cons 'c c)
           (cons 'd d)
           (cons 'rest rest))))

(define-syntax (test-macro stx)
   (syntax-case stx ()
     [(_ arg ...)
      (let ([args (map syntax->datum (syntax->list #'(arg ...)))])
        #`(quote #,(keyword-apply* keywords->alist args)))]))

; Try these:
; (test-macro)
; (test-macro 123)
; (test-macro 123 234 345)
; (test-macro #:a 123 #:b 234 345)
; (test-macro #:a 123 #:b 234 #:c 345)

Hope this helps,

-- Dave

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090106/0e38e53f/attachment.html>

Posted on the users mailing list.