On this page

0183. Match and option/result ergonomics

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

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.