Stream: beginners

Topic: Help with open and closed unions


view this post on Zulip Hannes (Sep 28 2023 at 03:33):

I have a function like this:

f : Dict Str [Hello, Goodbye, Neither], [Hello, Goodbye] -> Dict Str [Hello, Goodbye, Neither]
f = \d, x -> d |> Dict.update "a" (\_ -> Present x)

But I get this error:

── TYPE MISMATCH ───────────────────────────────────────────────────────────────

This 3rd argument to update has an unexpected type:

5│      f = \d, x -> d |> Dict.update "a" (\_ -> Present x)
                                           ^^^^^^^^^^^^^^^

The argument is an anonymous function of type:

    […] -> [Present [
        Goodbye,
        Hello,
    ], …]

But update needs its 3rd argument to be:

    […] -> [Present [
        Goodbye,
        Hello,
        Neither,
    ], …]

I know that the argument x can only be Hello or Goodbye, but the Dict's values can also contain Neither. Is there a way I can update this code to compile without widening the scope of the x argument? Thanks :)

view this post on Zulip Brendan Hansknecht (Sep 28 2023 at 04:06):

You would need to match on x and the select then correct value that dict expects.

view this post on Zulip Brendan Hansknecht (Sep 28 2023 at 04:07):

So with a when x is ....

view this post on Zulip Hannes (Sep 28 2023 at 04:15):

Thanks Brendan, I just tried this in the repl:

f : Dict Str [Hello, Goodbye, Neither], [Hello, Goodbye] -> Dict Str [Hello, Goodbye, Neither]
f = \d, x ->
    when x is
        Hello -> d |> Dict.update "a" (\_ -> Present Hello)
        Goodbye -> d |> Dict.update "a" (\_ -> Present Goodbye)

And the roc repl used all my laptop's available memory and then crashed :/

Is that the solution you meant? Thanks again :)

view this post on Zulip Brendan Hansknecht (Sep 28 2023 at 05:21):

That is one option, though I meant in the lambda to do the match.

view this post on Zulip Brendan Hansknecht (Sep 28 2023 at 05:21):

As for the memory issue....woah....definitely some sort of bug

view this post on Zulip Brendan Hansknecht (Sep 28 2023 at 05:22):

Can you file an issue, especially if that is a small reproducer?

view this post on Zulip Hannes (Sep 28 2023 at 06:33):

Do you mean like this?

f : Dict Str [Hello, Goodbye, Neither], [Hello, Goodbye] -> Dict Str [Hello, Goodbye, Neither]
f = \d, x ->
    d |> Dict.update "a" (
        \_ -> when x is
            Hello -> Present Hello
            Goodbye -> Present Goodbye
    )

If so, that also has the same memory issue :sweat_smile:

view this post on Zulip Hannes (Sep 28 2023 at 06:34):

I'll file an issue later today

view this post on Zulip Hannes (Sep 28 2023 at 08:30):

Created issue 5864

view this post on Zulip Richard Feldman (Sep 28 2023 at 11:50):

@Hannes thanks! Does it still reproduce if you remove the annotation?

view this post on Zulip Hannes (Sep 28 2023 at 12:35):

It crashes with or without the type annotation. I've added that to the issue on GH :+1:


Last updated: Jul 06 2025 at 12:14 UTC