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 :)
You would need to match on x
and the select then correct value that dict expects.
So with a when x is ....
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 :)
That is one option, though I meant in the lambda to do the match.
As for the memory issue....woah....definitely some sort of bug
Can you file an issue, especially if that is a small reproducer?
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:
I'll file an issue later today
Created issue 5864
@Hannes thanks! Does it still reproduce if you remove the annotation?
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