Hi! I'm doing AoC in Roc (and others) and handling Results has taken up an incredible portion of the time solving 1a. I know the inputs are valid, so I'd like to crash
on invalid inputs. However, when I do a match on an Ok Result, the crash in the Err would still evaluate. How should I use crash properly?
I ended up pulling off an absolute anti-pattern just to get it done
purifyStr = \x -> Result.withDefault x "AAAAA"
purifyInt = \x -> Result.withDefault x 999999
:confounded:
oh and this gives me
thread '<unnamed>' panicked at /build/source/crates/compiler/types/src/unification_table.rs:295:62:
index out of bounds: the len is 3561 but the index is 3561
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
want me to report it on the issue tracker?
Normally, I'd write something like this, if I understand you correctly:
parseInput = \input ->
parseResult = doStuffWith result
when parseResult is
Ok val -> val
Err err -> crash "Failed to parse input with err $(Inspect.toStr err)"
This is also what I did last year. But with the new try
syntax, it is so easy to handle the error case, that I don't feel the need to use crash
any more. You can just write:
input
|> parseInput # This returns a Result
|> try # This Removes the Result
|> solveAoC
|> Ok
The only requirement is, that you are calling this code in a function that returns a Result.
I'm working on improving try
at the moment. Please feel free to try
it out (harr harr), and let me know if you run into any issues!
oh, so that try
simplifies things like result of results? That's good enough yep
And also @Emil J I think that crash is because of your lefts_sorted
When I changed it to camelCase, it worked
We currently only do camelCase in Roc, but I'm surprised that you got such a deep error...
I don't think it's worth reporting an issue on because we're planning on moving to snake_case soon
@Emil J here's the recent announcement on usage of try
:
#announcements>New `return` and `try` keywords
Heads up, you can't currently use it in the same function as !
But that'll get fixed in the next month or so, I think
ok thanks!
Last updated: Jul 06 2025 at 12:14 UTC