I'm writing a function that calls several other functions that each return a Result
. If any of them return an Err
, I'd like to exit the function with an Err
. Is there a simple way to do that?
I can use Result.map
, but it feels a bit cumbersome, and harder to read. Ideally, I would just like an equivalent of the !
syntax sugar that exists for Task
, but applied to Result
.
Specifically, I'm trying to parse some text that looks like this: "1-3 a: blabla"
and produce a tuple like (1, 3, 'a', "blabla")
. It's just 3 calls to Str.split
and 2 calls to Str.toU64
, but the function doesn't look very clear right now. Here's a simplified example:
testthis = \aStr, bStr ->
aRes = Str.toI32 aStr
bRes = Str.toI32 bStr
when (aRes, bRes) is
(Ok a, Ok b) -> Ok (a + b)
_ -> Err BadInputs
This works, but it's a bit heavy, and it gets worse the more you have function calls returning Result
.
I'm hoping for something simpler, in the following spirit (not this syntax, but this kind of spirit):
testthis = \aStr, bStr ->
a = Str.toI32! aStr
b = Str.toI32! bStr
a + b
So we have had discussions on Zulip about providing syntax sugar a la Rust to do what you're asking for already, let me get you a link
https://github.com/roc-lang/roc/issues/6828
There's work in progress for Result.try
sugar: https://github.com/roc-lang/roc/pull/6844
It will be ?
for Result
as !
for Task
.
Otherwise you can use backpassing
So in short, your testthis
function will work once the Result.try
sugar is finished being implemented
Fantastic, thanks a lot! Perhaps that would be worth mentionning on the Error handling page in the docs?
I believe with backpassing it would look like this. The backpassing syntax would probably be removed, but I think it's still worth mentioning it to have more opinions when the last discussion of removal is brought up:
testthis = \aStr, bStr ->
a <- Str.toI32 aStr |> Result.try
b <- Str.toI32 bStr |> Result.try
Ok (a + b)
@Aurélien Geron yeah, it's definitely gonna be added once the feature is implemented, but it could definitely be added as a "future feature" tip!
Thanks Sam & Kiryl, that's super helpful. I'll submit a PR for a future feature tip.
Feel free to add me as a PR reviewer!
Looks like Github is down right now. :sick:
I've pushed 3 PRs:
Result
sLast updated: Jul 06 2025 at 12:14 UTC