resyntax
resyntax copied to clipboard
Replace conditionals in `match` with `#:when`
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.