answer 1) You are missing a dot in (~@ propthing), ie this will work:
(~@ . propthing)

minor question) is there a reason you are using with-syntax with syntax-parse?

answer 2) You may want to try "attribute". It's somewhat like
"syntax", except it returns false when there are unbound patvars, eg:

#lang racket
(require (for-syntax racket racket/syntax syntax/parse))
(define-syntax (foo stx)
  (syntax-parse stx
    [(foo name (~optional flag))
     #:with propthing (if (attribute flag)
                          #'(#:property prop:foo (delay "stuff"))
                          #'())
     #`(begin
         (define-values (prop:foo foo? foo-ref)
           (make-struct-type-property 'foo 'can-impersonate))
         (struct name (id) (~@ . propthing) #:transparent)
         (name 'bob))]))

(foo person)
(foo thing #f)

answer 3) A more "rackety" way would be if you included a part of the
output as the optional input. (this would invert your default case
though). This is the more natural use case for the ~? and ~@ patterns
because you no longer need the extra "if":

#lang racket
(require (for-syntax racket racket/syntax syntax/parse))
(define-syntax (foo stx)
  (syntax-parse stx
    [(foo name (~optional prop-val))
     #`(begin
         (define-values (prop:foo foo? foo-ref)
           (make-struct-type-property 'foo 'can-impersonate))
         (struct name (id) (~? (~@ #:property prop:foo (delay
prop-val))) #:transparent)
         (name 'bob))]))

(foo person)
(foo thing "stuff")


On Wed, May 15, 2019 at 10:56 AM David Storrs <david.sto...@gmail.com> wrote:
>
> I'd like to find a general mechanism, when writing macro code, to say "If 
> this optional argument was supplied, generate this code.  If not, generate 
> this other code", where "this other code" might be nothing at all.  I feel 
> like this should be simple, but my brain is failing.
>
> As an example, consider a macro that generates a struct; the macro has an 
> optional argument that controls whether a property is added, and it defaults 
> to 'add it'. I thought I could do the following, but it doesn't compile 
> because the bits aren't spliced properly and I'm not sure what I'm missing.  
> Note that the syntax->datum part is because I want it to default to #t 
> whereas (attribute missing-optional-value) would be #f and offer no way to 
> distinguish between a missing argument and an explicit #f.  Again, I feel 
> like there should be a simpler way.
>
> #lang racket
> (require (for-syntax racket racket/syntax syntax/parse))
> (define-syntax (foo stx)
>   (syntax-parse stx
>     [(foo name  (~optional arg:boolean))
>      (with-syntax ([propthing
>                     (if (syntax->datum #'(~? arg #t))
>                         #'(#:property prop:foo (delay "stuff"))
>                         #'())])
>        #`(begin
>            (define-values (prop:foo foo? foo-ref)
>              (make-struct-type-property 'foo 'can-impersonate))
>            (struct name (id) (~@ propthing) #:transparent)
>            (name 'bob)))]))
>
> (foo person)
> (foo thing #f)
>
>
>
>
>
> --
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/CAE8gKofM%3Dep0B%2BY0YXOq5uqooh-jDTBr7mZ9yDK%2BFrVVVbEHjA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAFfiA1KCWzcg67HxkvUSikuwwZAXkeORmmz_2W2zB6MOJHW9UQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to