[racket] Macros: dealing with optional elements

From: Laurent (laurent.orseau at gmail.com)
Date: Wed Sep 25 06:17:30 EDT 2013

On Wed, Sep 25, 2013 at 11:31 AM, Konrad Hinsen
<konrad.hinsen at fastmail.net>wrote:

>
>    (define-syntax (foo stx)
>      (syntax-parse stx
>                    [(_ id:id (~optional super:id) (field:id ...))
>                     (if (attribute super)
>                         #'(struct id super (field ...))
>                         #'(struct id (field ...)))]))



Unsyntax-splicing to the rescue!

#lang racket
(require (for-syntax syntax/parse))

(define-syntax (foo stx)
  (syntax-parse stx
    [(_ id:id (~optional super:id) (field:id ...))
     #`(struct id
         #,@(if (attribute super)
                (list #'super)
                '())
         (field ...))
     ]))

(struct A (a))

(foo B (b))
(foo C A (c))

(A 1)
(B 2)
(C 3 4)


Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130925/b9d6d7ab/attachment.html>

Posted on the users mailing list.