# Maybe definitely?

**URL:** <https://racket.discourse.group/t/maybe-definitely/3639>\
**Category:** Show & Tell\
**Created:** [March 20, 2025, 11:42am UTC](https://racket.discourse.group/t/maybe-definitely/3639 "2025-03-20T11:42:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [March 20, 2025, 11:42am UTC](https://racket.discourse.group/t/maybe-definitely/3639/1 "2025-03-20T11:42:42Z")

</div>

Hi, Racket Discourse.

It is a public holiday in RSA tomorrow, so today is Friday, haha.

I thought I would share a riff on a `pattern-expander`. From the [`phc-toolkit`](https://docs.racket-lang.org/phc-toolkit/syntax-parse_helpers.html#%28form._%28%28submod._%28lib._phc-toolkit%2Fsyntax-parse..rkt%29._typed%29._~7emaybe%29%29), there is a `pattern-expander` called `~maybe` that looks like:

```scheme
(~optional {~seq pattern ...})

```

I have been using this for recognizing parts of a URL in a constructor-macro, which works great for sequences that may or may not be there.

But, sometimes you'd like to know whether the pattern actually matched, or whether it fell through. In my case, because I use this to keep track of whether a pattern's result should be used to `set!` a `url`'s field or not, when applicable.

A simple modification to this (perhaps there is an even simpler one?), to allow this check, looks like this:

```scheme
(define-splicing-syntax-class
  none (pattern {~seq}))

(define-syntax ~maybe
  (pattern-expander
   (lambda (stx)
     (syntax-case stx ()
       [(_ isa pat ...)
        #'(~optional {~seq (~var isa none) pat ...} #:defaults ([isa #false]))]))))

```

Then, you can write:

```scheme
(define-splicing-syntax-class
  fragment
  #:description
  "an optional, keyword-delimited url fragment"
  #:attributes
  ([fragment 0] [$fragment 0] [fragment? 0])
    
  (pattern {~maybe fragment? #:focus :fragment-value}
    #:attr fragment #'(~? val #false)
    #:attr $fragment #'(~? arg ())))

```

where the `val` and `arg` attributes come from `:fragment-value`, this case.

Mooiloop!
