Stream: beginners

Topic: Early exit for when an Err is encountered?


view this post on Zulip Aurélien Geron (Jul 31 2024 at 10:46):

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

view this post on Zulip Sam Mohr (Jul 31 2024 at 10:50):

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

view this post on Zulip Sam Mohr (Jul 31 2024 at 10:50):

https://github.com/roc-lang/roc/issues/6828

view this post on Zulip Kiryl Dziamura (Jul 31 2024 at 10:50):

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

view this post on Zulip Sam Mohr (Jul 31 2024 at 10:51):

So in short, your testthis function will work once the Result.try sugar is finished being implemented

view this post on Zulip Aurélien Geron (Jul 31 2024 at 10:56):

Fantastic, thanks a lot! Perhaps that would be worth mentionning on the Error handling page in the docs?

view this post on Zulip Kiryl Dziamura (Jul 31 2024 at 10:57):

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)

view this post on Zulip Sam Mohr (Jul 31 2024 at 10:59):

@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!

view this post on Zulip Aurélien Geron (Jul 31 2024 at 19:45):

Thanks Sam & Kiryl, that's super helpful. I'll submit a PR for a future feature tip.

view this post on Zulip Sam Mohr (Jul 31 2024 at 21:51):

Feel free to add me as a PR reviewer!

view this post on Zulip Aurélien Geron (Jul 31 2024 at 21:52):

Looks like Github is down right now. :sick:

view this post on Zulip Aurélien Geron (Aug 01 2024 at 01:15):

I've pushed 3 PRs:


Last updated: Jul 06 2025 at 12:14 UTC