liveBook · Manning

Exhaustiveness

As explained in F# in Action: A Revised Edition of Get Programming with F#

Exhaustiveness checking is compile-time verification that pattern matching handles every possible case of a type.

When a contact method can be one of a fixed set of cases, a match should handle every case. With an F# discriminated union, the compiler knows that set and warns when a pattern is missing. A string field cannot offer the same check: another string could always be passed in. section 8.3.3

Handle each case explicitly

For `type Instrument = Guitar | Drums | Bass`, a `play` function can match `Guitar`, `Drums`, and `Bass` separately. If a fourth case is added, an incomplete match produces a warning until that case has a handler. Replacing `Bass` with `_` saves a line but also lets future cases fall into that branch without a warning. section 14.1.1

The check reaches into nested unions. If `Sms` contains a telephone number that is either `Local` or `International`, matching only `Sms (Local number)` leaves the international case uncovered; the warning identifies it. section 8.3.4

Listing 8.10 Modeling a nested (recursive) relationship with unions
type TelephoneNumber =                     #1
    | Local of number:string
    | International of countryCode:string * number:string
 
type ContactMethod =
    | // ... other cases omitted
    | Sms of TelephoneNumber               #2
 
let smsContact = Sms (Local "123-4567")    #3

The `Sms` case contains another union, so its local and international forms can be matched separately. section 8.3.4

For a loan decision, replacing integer and Boolean inputs with bounded unions makes the match and its function signature more readable. Prefer explicit cases when a new case should demand a decision; use a wildcard only when grouping future cases is intentional. Completeness checking does not verify that each branch returns the right answer, so tests can still help with branch behavior. section 8.3.4 section 14.1.1

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Demonstrates missing-case warnings for added and nested union cases, then explains how wildcards can hide them.
Examples
Uses contact methods with local and international SMS numbers, loan-decision types, and an instrument-matching function.
What it assumes
Builds on F# discriminated unions and match expressions, illustrating completeness with explicit cases.
What you can do after
Apply it via F# compiler warnings to find unhandled union cases while retaining tests for branch behavior.
How it connects
Links discriminated unions and pattern matching to domain modeling, wildcard patterns, and testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.