Add syntax sugar that translates ? to Result.try #6828
Labels
can
Canonicalization
enhancement
New feature or request
intermediate issue
Likely good for someone who has completed a few other issues
parsing
Issue following from discussion in Zulip chat.
We want to add Rust's try operator, the
?
, to Roc, making early returns onErr
cleaner. This will work just like!
works when it desugars toTask.await
.There are many benefits to having
?
added to Roc, including "flatter" code, better readability, and better concision, but these could also be achieved if ! was extended to work forResult
s or even anything implementing a hypotheticalAndThen
ability. However, the mental overhead of understanding that a return type'sAndThen
implementation is being leveraged, as well as the teach-ability damage to the language, make generalizing the ! operator cost-prohibitive. By having!
just forTask
s and?
just forResult
s, we get the cleanliness benefits of each operator without making the language much harder to learn, and we prevent potential exploitation of a generalized!
operator.This also paves the way for the eventual deprecation and removal of backpassing from the language.
Some things we should consider when implementing this operator:
!
operator, but will instead translate toResult.try
every time?
is used on isn't a tag union, but also if there are extra or missing variants from[Ok _, Err _]
The text was updated successfully, but these errors were encountered: