There are several ways to do this, and they each have their uses depending on 
what you want to do with it.

One is with a syntax-class:
#lang racket
(require syntax/parse)
(define-splicing-syntax-class my-keyword
  [pattern (~seq #:my-keyword) #:attr kw #t]
  [pattern (~seq) #:attr kw #f])
(syntax-parse #'()
  [(kw:my-keyword) (attribute kw.kw)])

Another is with an ~and within the ~optional pattern and using attribute 
instead of syntax, which is the one that
http://docs.racket-lang.org/syntax/More_Keyword_Arguments.html recommends:
#lang racket
(require syntax/parse)
(syntax-parse #'()
  [((~optional (~and kw #:my-keyword))) (attribute kw)])
Or if you want explicit control over what the default is:
#lang racket
(require syntax/parse)
(syntax-parse #'()
  [((~optional (~and kw #:my-keyword) #:defaults ([kw #f]))) (attribute kw)])

Or you could use a pattern like this:
#lang racket
(require syntax/parse)
(syntax-parse #'()
  [((~and (~seq kw ...) (~optional #:my-keyword))) #'(kw ...)])

And you can define a pattern expander that expands to one of those if you want.

Or there might be a better way of doing this that I don’t know about.


On Apr 17, 2015, at 8:38 AM, Konrad Hinsen <konrad.hin...@fastmail.net> wrote:

> Hi everyone,
> 
> my question looks like something straightforward, but I have been
> reading through the documentation for a while without finding anything
> clearly helpful.
> 
> I want to define some syntax with an optional keyword but no arguments
> behind it. Something like #:mutable in struct. The pattern seems
> obvious:
> 
>   (~optional #:my-keyword)
> 
> but I don't see how I can detect the presence or absence of the
> keyword in the code that I generate. There is no attribute for
> which I could define a default.
> 
> Konrad.
> 
> 
> -- 
> 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.

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