Stream: beginners

Topic: result unwrap?


view this post on Zulip itmuckel (Mar 31 2023 at 07:16):

Hi, is there a way to directly Result.unwrap a result without specifying a default value? I'm doing advent of code and I just want my program to crash if something doesn't work as intended. :nerd:

view this post on Zulip Nick Hallstrom (Mar 31 2023 at 07:50):

You could just make your own:

unwrap = \result ->
    when result is
        Ok a -> a
        Err _ -> crash "oopsie whoopsie"

I wonder if it would be possible to change crash to be lazy, so that something like Result.unwrap result (crash "uh oh") could work :thinking:

view this post on Zulip Asier Elorz (he/him) (Mar 31 2023 at 10:41):

In my advent of code I was using a slightly different version of unwrap that takes a string as an input. Something along the lines of

unwrap = \result error_message
    when result is
        Ok a -> a
        Err _ -> crash error_message

I think that is what you were looking for, @Nick Hallstrom ?

view this post on Zulip Richard Feldman (Mar 31 2023 at 11:26):

you can also do result |> Result.mapErr \_ -> crash "kaboom"

view this post on Zulip Richard Feldman (Mar 31 2023 at 11:35):

we intentionally didn't add Result.unwrap to the language because making crashing the most convenient way to handle errors wouldn't be a great incentive to create :big_smile:


Last updated: Jul 06 2025 at 12:14 UTC