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:
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:
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 ?
you can also do result |> Result.mapErr \_ -> crash "kaboom"
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