Cleanest way to 'if/then' inside a macro?

47 views
Skip to first unread message

David Storrs

unread,
May 15, 2019, 10:56:24 AM5/15/19
to Racket Users
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)





Stephen Chang

unread,
May 15, 2019, 11:48:58 AM5/15/19
to David Storrs, Racket Users
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")
> --
> 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...@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.

David Storrs

unread,
May 15, 2019, 12:30:32 PM5/15/19
to Stephen Chang, Racket Users
On Wed, May 15, 2019 at 11:48 AM Stephen Chang <stc...@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.
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."

Stephen Chang

unread,
May 15, 2019, 12:59:02 PM5/15/19
to David Storrs, Racket Users
> 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?

The #:with syntax-parse "pattern directive" does the same thing, but
with less parens.
https://docs.racket-lang.org/syntax/stxparse-specifying.html?#%28tech._pattern._directive%29


> 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.

I see. IMO it's awkward because it's trying to handle three cases for
only two possible outputs. I think the more Racket-standard way to do
this is to only have 2 possibilities: explicit argument or no
argument. See the struct options for examples, eg #:mutable or
#:omit-define-syntaxes
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAE8gKocZmY6W1iuH77GHEM7_G6uePtAkCUZsBWnsijw60tW8YA%40mail.gmail.com.

David Storrs

unread,
May 15, 2019, 5:30:36 PM5/15/19
to Stephen Chang, Racket Users
On Wed, May 15, 2019 at 12:59 PM Stephen Chang <stc...@ccs.neu.edu> wrote:
> 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?

The #:with syntax-parse "pattern directive" does the same thing, but
with less parens.
https://docs.racket-lang.org/syntax/stxparse-specifying.html?#%28tech._pattern._directive%29


Fair do.  I suppose I never got in the habit of using #:with because the 'let'-like structure of with-syntax felt so familiar.


> 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.

I see. IMO it's awkward because it's trying to handle three cases for
only two possible outputs. I think the more Racket-standard way to do
this is to only have 2 possibilities: explicit argument or no
argument. See the struct options for examples, eg #:mutable or
#:omit-define-syntaxes

Yeah, I agree that it's awkward.  It's a backwards compatibility thing -- when I first wrote the module (struct-plus-plus) I had it generate reflection information by default.  Then I ran into the "can't use prefab if there's a property" issue so now I want to add a switch that allows for omitting the reflection information, but it needs to create the information when the switch isn't there so as not to break existing code.  I could phrase the switch in the negative, e.g. #:no-reflection, but I disprefer that.  I suppose #:omit-reflection would work -- it's a positive construction of a negative action.  I'll do that. Thanks.
Reply all
Reply to author
Forward
0 new messages