> On Mar 20, 2017, at 11:04 AM, Philip McGrath <phi...@philipmcgrath.com> wrote:
> 
> Using expr/c to attach a contract to a macro sub-pattern doesn't seem to work 
> with ~optional, even when the attribute is bound with #:defaults.
> 
> For example, this program:
> #lang racket 
> (require (for-syntax syntax/parse))
> (define-syntax (example stx)
>   (syntax-parse stx
>     [(_ (~optional (~seq #:return val)
>                    #:defaults ([val #'42])))
>      #:declare val (expr/c #'(or/c list? #f))
>      #'val.c]))
> (example)
> reports the following error:
> val.c: bad attribute value for syntax template
>   attribute value: #f
>   expected for attribute: syntax
>   sub-value: #f
>   expected for sub-value: syntax in: val.c
> 
> Is there a better way to do this? 

This is because the `val` in the #:defaults is treated as just an attribute 
name, not a variable pattern, and syntax-classes like `expr/c` can only apply 
to variable patterns. One way to work around this is to use ~or and ~parse to 
put it in a pattern position:

#lang racket 
(require (for-syntax syntax/parse))
(define-syntax example
  (syntax-parser
    [(_ (~or (~seq #:return val)
             (~and (~seq) (~parse val #'42))))
     #:declare val (expr/c #'(or/c list? #f))
     #'val.c]))
(example)

This raises the contract violation you expected.

Alex Knauth

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to