0183. Match and option/result ergonomics
- Status: Proposed (pending maintainer)
- Date: 2026-11-07
- Provisionally supersedes the union wildcard restriction in ADR-0022; otherwise retains it.
- Extends ADR-0025; integer matches in ADR-0065 are unchanged.
Context
Common string dispatch and handling seeded unions should not require repetitive conditionals or exhaustive matches for straightforward defaults and tests.
Decision
String matches allow literal alternatives and require one final
unqualified _ arm without binders. Scrutinees run
once; first match wins using byte equality, including NUL.
Patterns use ordinary string escapes and escaped braces, but no
interpolation (E0513). Other invalid patterns or defaults use
E0517; missing defaults E0518, generalized without changing
integer diagnostic wording.
Union matches allow one final unqualified _ arm
without binders or alternatives, covering remaining variants. A
wildcard after complete explicit coverage and other malformed
wildcards are E0503. Duplicate explicit variants remain E0504;
literal union patterns remain E0513.
option<T> adds
unwrap_or(T) -> T,
is_some() -> bool,
is_none() -> bool; result<T, E>
adds unwrap_or(T) -> T,
is_ok() -> bool, is_err() -> bool.
The default has exactly type T and is eager: receiver first, then
default, each evaluated once, even on success. Builtins take
precedence over interfaces; E0662 help lists the supported
methods.
Lower methods to existing Switch/Payload operations and
wildcard arms to remaining tags. Strings reuse scalar match
control flow and byte equality in both backends; canonical hashes
record decoded string patterns. No new IR operations or
dependencies. Formatter output for existing programs is unchanged,
so FMT_VERSION stays 1 under ADR-0161; the corpus
digest was rehashed at version 1 for the new programs.
Alternatives considered
- Require explicit union variants and string conditionals: preserves strict coverage on every variant but makes common fallback cases verbose.
- Add guards, deep patterns or
map_err: defer coverage complexity and the extra block/control-flow surface and checking; there are no closure values. - Make defaults lazy: requires a new deferred computation mechanism and changes ordinary argument evaluation.
Consequences
Wildcard arms can hide newly added union variants; explicit arms remain available for exhaustive change detection. Both backends must agree on evaluation and bytes.