mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-23 08:48:03 +00:00
The ? operator now desugars expr? to a match expression that unwraps Ok
values and early returns Err values:
match expr {
Ok(#ok) => #ok,
Err(#err) => return Err(#err),
}
Implementation details:
- Use pre-interned identifiers (#ok, #err) for synthetic variables
- Use pre-interned Ok/Err tag names from CommonIdents
- Create applied_tag patterns for Ok and Err
- Build match branches with proper scope isolation
- Mark synthetic patterns as used to avoid unused variable warnings
- Create e_lookup_local, e_tag, and e_return expressions for branch bodies
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
19 lines
459 B
Text
19 lines
459 B
Text
app [main!] { pf: platform "./platform/main.roc" }
|
|
|
|
import pf.Stdout
|
|
|
|
# Tests the `?` operator for error propagation.
|
|
# The operator unwraps Ok values or early-returns Err values.
|
|
|
|
get_greeting : {} -> Try(Str, [ListWasEmpty])
|
|
get_greeting = |{}| {
|
|
first = List.first(["hello"])?
|
|
Ok(first)
|
|
}
|
|
|
|
main! = || {
|
|
match get_greeting({}) {
|
|
Ok(greeting) => Stdout.line!(greeting)
|
|
Err(ListWasEmpty) => Stdout.line!("List was empty!")
|
|
}
|
|
}
|