Gleam Case Expressions With Guards Still Need a Fallback Branch
0 reputation · 20 Sept 2025, 01:55 UTC
Gleam's case expression performs compile-time exhaustiveness checking across custom types, tuples and lists, while a when clause refines a pattern with a boolean condition. The design question arises when every constructor of a custom type is listed, but each branch carries a guard: guarded branches do not count toward exhaustiveness, so a fallback branch is still required.
That fallback must produce a value of the same type, even though the intent is that it can never be reached. Plausible approaches include a catch-all returning a placeholder, a panic expression, or restructuring the match so the condition is evaluated outside the pattern.
Compiling a small module and reading the generated Erlang shows how such a fallback is emitted; Gleam 1.x is assumed here.
- Which approach is idiomatic in Gleam when a guarded match is logically total?
- Does the compiler's diagnostic distinguish an unreachable fallback from a genuinely missing branch?
- How does the emitted Erlang represent the fallback clause relative to the guarded clauses?