There is someone at every door

Write down the visitors

A match expression is a little row of doors. The compiler wants to know whether every possible visitor can get through one of them.

enum Weather {
  Clear,
  Rain
}

plan(weather: Weather) -> String {
  match weather {
    Weather.Clear -> "take a walk"
    Weather.Rain -> "work on Pooka"
  }
}

main() {
  assert plan(Weather.Rain) == "work on Pooka"
}

A missing door is a real decision

constructors:  Clear, Rain
covered:       Clear
still missing: Rain

This is a coverage sketch. Adding a wildcard covers the remaining input; it does not explain what policy that fallback should implement.

There are two constructors here, and both have somewhere to go. If another constructor joins the enum, the compiler should ask us to revisit the match.

A final wildcard could silence that request. Sometimes a fallback is exactly the intended behaviour. Sometimes it is a cupboard in which a missing decision has been hidden. The compiler cannot choose the policy for us, but it can make the uncovered case visible.

Look inside the patterns

Checking coverage becomes more interesting when patterns contain other patterns. Matching a constructor is not enough if its fields still have uncovered combinations. A Boolean field has two possibilities; a nested enum introduces its own branches.

Guards need care as well. An arm for a number only when it is positive does not cover every number. The presence of a recognisable outer pattern cannot make a condition disappear.

Pooka’s closed class families make a related promise. Subclasses belong with their base class in the same module. That gives the checker a bounded family to reason about. Interfaces describe capabilities instead; they do not provide the same closed inventory of implementations.

I like exhaustiveness because it turns a future edit into a present conversation. Add a new shape, and the places that have opinions about all the shapes can ask for attention.

It cannot prove the plan is wise. It can stop the rain from arriving to find that we built no door for it.

— Peregrine Finch

· pooka, types, pattern-matching