[plt-scheme] Keyword arguments using syntax/parse

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Sat Aug 29 05:13:16 EDT 2009

Hi all,

I'm experimenting with syntax/parse as a way of implementing optional  
keyword arguments with default values.

I've included some test code below. I'm new to this library so I want  
to make sure I'm not missing anything. Is this the intended approach  
for this kind of thing?

Many thanks,

-- Dave

; keyword-test.ss =================================

#lang scheme

(require (for-syntax syntax/parse))

; Example macro -----------------------------------

; (_ [#:a expr] [#:b expr])
; Expands to (list a b) where a and b:
;   - are either specified using #:a and #:b;
;   - or take on default values 'default-a and 'default-b.

(define-syntax (test stx)
   (syntax-parse stx
     [(_ (~or (~optional (~seq #:a user-a) #:name "#:a keyword")
              (~optional (~seq #:b user-b) #:name "#:b keyword")) ...)

      (with-syntax ([a (or (attribute user-a) #''default-a)]
                    [b (or (attribute user-b) #''default-b)])

        #'(list a b))]))

; Test code ---------------------------------------

(test)             ; => '(default-a default-b)
(test #:a 1)       ; => '(1 default-b)
(test #:a 1 #:b 2) ; => '(1 2)

; =================================================

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090829/62f9ea4b/attachment.html>

Posted on the users mailing list.