Stream: beginners

Topic: ✔ How does ? get desugared to Result.try?


view this post on Zulip Nathan Kramer (Oct 01 2024 at 06:12):

Hi all,

I've been trying to wrap my head around the ? suffix.
As far as I can tell, it unwraps the result and "returns early" in the error case... is that right?

How would I write this code from the tutorial with Result.try rather than ? ?

getLetter : Str -> Result Str [OutOfBounds, InvalidNumStr]
getLetter = \indexStr ->
    index = Str.toU64? indexStr
    List.get ["a", "b", "c", "d"] index

I can't quite figure it out. I tried something like this, but it doesn't really map onto the assignment to index above:

getLetter : Str -> Result Str [OutOfBounds, InvalidNumStr]
getLetter = \indexStr ->
    Result.try (Str.toU64 indexStr) \index ->
        List.get ["a", "b", "c", "d"] index

view this post on Zulip Brendan Hansknecht (Oct 01 2024 at 06:28):

In current roc, I'm pretty sure those two functions are 100% equivalent

view this post on Zulip Brendan Hansknecht (Oct 01 2024 at 06:28):

The first desugars into the second

view this post on Zulip Nathan Kramer (Oct 01 2024 at 07:07):

Oh I see! Thank you :man_bowing:

view this post on Zulip Notification Bot (Oct 01 2024 at 07:11):

Nathan Kramer has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC