resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

Replace conditionals in `match` with `#:when`

Open jackfirth opened this issue 1 year ago • 0 comments

Saw a few cases in racket/typed-racket#1410 where #:when would clean up various bits of pattern matching code:

#lang resyntax/test

test: "original code should be refactorable to new code"
--------------------
#lang racket
(define (f pt)
  (match pt
    [(list x y)
     (if (> x y)
         (list y x)
         pt)]
    [(list) '()]))
--------------------
--------------------
#lang racket
(define (f pt)
  (match pt
    [(list x y)
     #:when (> x y)
     (list y x)]
    [(list x y) pt]
    [(list) '()]))
--------------------

This should probably only trigger when the pattern is small. It should also only trigger on match expressions that already have multiple branches, since the single-branch case would be clearer as a match-define. A two-case cond instead of an if should also count.

jackfirth avatar Nov 08 '24 19:11 jackfirth