[plt-scheme] Keyword arguments using syntax/parse

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Sun Aug 30 05:12:12 EDT 2009

Ryan Culpepper wrote:
> Dave Gurnell wrote:
>> 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?
>
> Yes, that's right. I intend to streamline this a little more soon,  
> but for now that's how to do it.
>
> Here's another way that you might find more pleasant: it separates  
> the options out into a "splicing syntax class":
>
> (define-syntax (test stx)
>
>  (define-splicing-syntax-class options
>    #:attributes (a b)
>    (pattern (~seq (~or (~optional (~seq #:a user-a)
>                                   #:name "#:a keyword")
>                        (~optional (~seq #:b user-b)
>                                   #:name "#:b keyword")) ...)
>             #:with a (or (attribute user-a) #''default-a)
>             #:with b (or (attribute user-b) #''default-b)))
>
>  (syntax-parse stx
>    [(_ opt:options)
>     #'(list opt.a opt.b)]))
>
> Ryan

That's very nice.

syntax/parse is awesome. I just managed to reduce two pages of parsing  
code by a factor of four in length.

Keep up the good work!

-- Dave



Posted on the users mailing list.