Stream: beginners

Topic: accessing unbound value crashes?


view this post on Zulip Bryan Maass (Feb 20 2024 at 04:40):

Hello, I'm getting a thread panic when running the following in my repl (M1 mac, on roc nightly pre-release, built from commit 943d7a3cdf on Fri Feb 16 09:13:04 UTC 2024)

when List.get ["a", "b", "c"] index is
    Ok str -> "I got this string: $(str)"
    Err OutOfBounds -> "That index was out of bounds, sorry!"

view this post on Zulip Anton (Feb 20 2024 at 10:52):

Hi @Bryan Maass :)

Can you provide a full self-contained reproduction?

I was not able to reproduce the panic when executing:

app "helloWorld"
    packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.8.1/x8URkvfyi9I0QhmVG98roKBUs_AZRkLFwFJVJ3942YA.tar.br" }
    imports [pf.Stdout]
    provides [main] to pf

main =
    index = 4

    resultStr =
        when List.get ["a", "b", "c"] index is
            Ok str -> "I got this string: $(str)"
            Err OutOfBounds -> "That index was out of bounds, sorry!"

    Stdout.line resultStr

It also worked with index 0

view this post on Zulip Bryan Maass (Feb 20 2024 at 21:56):

Thanks for checking it with me. Oh, you know, I'm not sure I had index bound in that example!

view this post on Zulip Bryan Maass (Feb 20 2024 at 21:57):

It totally works when I don't reference an unbound value!

view this post on Zulip Anton (Feb 21 2024 at 09:31):

It still should not panic if index is undefined, I get a nice error message if I try that. Can you share the code that caused a panic?


Last updated: Jul 06 2025 at 12:14 UTC