Skip to content

Sprout v0.0.26 — pattern matching

Choose a tag to compare

@fizzexual fizzexual released this 12 Jun 18:53
· 18 commits to main since this release

Pattern matching — clear multi-way dispatch, with destructuring.

match value: checks a value against is arms in order and runs the first that fits — comparing it or pulling a list/map apart — plus an optional otherwise.

match command:
    is "start":     show "go"            # a value, compared with ==
    is "stop":      show "halt"
    is [a, b]:      show a + " & " + b    # a 2-item list: binds a, b
    is {name, age}:                       # a map with those keys: binds name, age
        show name + " is " + age
    otherwise:      show "no idea"

Three kinds of pattern

  • Value — any expression (is 0, is "x", is yes, is nothing, even is [1, 2]), matched with ==.
  • List-destructureis [a, b] matches a list of exactly that length and binds each item to a name (is [] matches the empty list).
  • Map-destructureis {name, age} matches a map that has those keys (a superset is fine) and binds each to a same-named variable (is {} matches only an empty map).

The rule of thumb: bare names in [ ]/{ } destructure; anything else ([1, 2], {a: 1}) is a value compared with ==. Bindings are local to their arm; give/stop/skip flow through arm bodies; if nothing matches and there's no otherwise, the match does nothing.

Hardened by review

A 3-lens adversarial review (+ verification, 28 agents) caught a real bug before release: is {}: was matching any non-empty map (and shadowing later arms) — now it matches only an empty map, symmetric with is []:. 24 over-flags correctly rejected.

33-check test suite; CI green on Linux, macOS, and Windows. Windows installer attached.

Next: the pipe operator — the last of the four.