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.

Reply via email to