Stream: beginners

Topic: Crashing


view this post on Zulip Emil J (Dec 02 2024 at 08:59):

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?

view this post on Zulip Emil J (Dec 02 2024 at 08:59):

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:

view this post on Zulip Emil J (Dec 02 2024 at 09:01):

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?

repro.roc

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:02):

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)"

view this post on Zulip Oskar Hahn (Dec 02 2024 at 09:07):

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.

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:09):

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!

view this post on Zulip Emil J (Dec 02 2024 at 09:09):

oh, so that try simplifies things like result of results? That's good enough yep

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:12):

And also @Emil J I think that crash is because of your lefts_sorted

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:12):

When I changed it to camelCase, it worked

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:12):

We currently only do camelCase in Roc, but I'm surprised that you got such a deep error...

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:13):

I don't think it's worth reporting an issue on because we're planning on moving to snake_case soon

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:14):

@Emil J here's the recent announcement on usage of try:

#announcements>New `return` and `try` keywords

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:14):

Heads up, you can't currently use it in the same function as !

view this post on Zulip Sam Mohr (Dec 02 2024 at 09:14):

But that'll get fixed in the next month or so, I think

view this post on Zulip Emil J (Dec 02 2024 at 09:23):

ok thanks!


Last updated: Jul 06 2025 at 12:14 UTC