[racket] Macros with Flexible Sub-Forms like (class...)

From: Scott Klarenbach (scott at pointyhat.ca)
Date: Sat Feb 23 15:34:58 EST 2013

Thanks Danny.  There's also something I recall for validation, as in the
docs for (define/override...) where usage outside of a (class...) form is a
syntax error.  I'll hunt...:)

On Sat, Feb 23, 2013 at 12:19 PM, Danny Yoo <dyoo at hashcollision.org> wrote:

> On Sat, Feb 23, 2013 at 12:22 PM, Scott Klarenbach <scott at pointyhat.ca>
> wrote:
> > What is the best approach for creating a macro that parses sub-forms in
> any
> > order, similar to the way (class ...) works in Racket?
>
>
> I believe that there's support in the syntax-parse library to parse
> these sub-forms in any order.  Let me see if I can find the
> appropriate documentation...  Ah, here:
>
>     http://docs.racket-lang.org/syntax/Optional_Keyword_Arguments.html
>
>
> The documentation here focuses on parsing optional keyword arguments
> that can be consumed in any order.  The underlying mechanism should
> work with non-keywords as well.
>
>
> For example:
>
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;
> #lang racket
>
> (require (for-syntax syntax/parse))
>
> (struct person (name age) #:transparent)
>
> ;; Accept name and age in any order, or even omit them:
> (define-syntax (new-person stx)
>   (syntax-parse stx
>     [(_ (~or (~optional ((~datum name) name) #:defaults ([name #'"Jane
> Doe"]))
>              (~optional ((~datum age) age) #:defaults ([age #'#f])))
>         ...)
>      #'(person name age)]))
>
> (new-person)
>
> (new-person (name "Pen Pen"))
>
> (new-person (age 48) (name "Gendo Ikari"))
> ;;;;;;;;;;;;;;;;;;;;;;;;;
>



-- 
Talk to you soon,

Scott Klarenbach

PointyHat Software Corp.
www.pointyhat.ca
p 604-568-4280
e scott at pointyhat.ca
200-1575 W. Georgia
Vancouver, BC V6G2V3

_______________________________________
To iterate is human; to recur, divine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130223/90603826/attachment-0001.html>

Posted on the users mailing list.