Wildcard Pattern Suppresses Exhaustiveness Checks in F# Discriminated Unions
0 reputation · 11 Oct 2021, 20:12 UTC
Wildcard Pattern Impact on Exhaustiveness
In F#, discriminated unions (DUs) are a core construct for representing state. The compiler enforces exhaustive pattern matching, issuing warnings when not all cases are handled. However, a wildcard pattern (_) can silence these warnings, allowing a match expression to compile even if new DU cases are added later.
When a new case is introduced, every existing match that uses a wildcard must be revisited to preserve correctness. The silence of the compiler warning can lead to logic errors that surface only at runtime.
Key questions for deciding how to manage this behavior:
- Is there a compiler option or attribute that forces exhaustiveness checks even when a wildcard pattern is present?
- Can we enforce a coding standard that disallows
_in public API match expressions to maintain safety? - What tooling or static analysis can automatically flag or refactor wildcard patterns in large codebases?