On Wed, May 15, 2019 at 11:48 AM Stephen Chang <stch...@ccs.neu.edu> wrote:
> answer 1) You are missing a dot in (~@ propthing), ie this will work: > (~@ . propthing) > Ah, nice I missed that in the docs. Thanks, Stephen. > minor question) is there a reason you are using with-syntax with > syntax-parse? > For the same reasons I would use a `let` around a code block -- it's an easy way to isolate computation and create reusable elements. What would a more normal way be when using 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: > 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 (which should be interpreted as #t) and an explicit #f. > #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") > Yep. That's how I would have done it if the prop-val were something simple, but this example was highly simplified for clarity In point of fact the contents of the `delay` are going to be a significant pile of reflectance data built off the struct transformer. Not something that the user should care about aside from saying "yes, do / don't include this." > > 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/CAE8gKocZmY6W1iuH77GHEM7_G6uePtAkCUZsBWnsijw60tW8YA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.