Stream: beginners

Topic: Can not pattern match Result


view this post on Zulip Ben Plotke (Mar 12 2024 at 13:04):

Hi,

I have not been successful in destructuring Results. If, for example, I try

somefunc : Result Str Str -> Str
somefunc = \ra ->
    when ra is
        OK a -> a
        Err r -> r

I get

── TYPE MISMATCH in sequence.roc ───────────────────────────────────────────────

The branches of this when expression don't match the condition:

 8│>      when ra is
 9│           OK a -> a
10│           Err r -> r

This ra value is a:

    Result Str Str

But the branch patterns have type:

    [
        Err *,
        OK *,
    ]

The branches must be cases of the when condition's type!

Am I doing something wrong?

Thanks!

view this post on Zulip Anton (Mar 12 2024 at 13:04):

Hi @Ben Plotke,
It needs to be Ok instead of OK

view this post on Zulip Anton (Mar 12 2024 at 13:06):

I've seen several similar issues over the last couple of days, what do you think of a tool like this to prevent them?

view this post on Zulip Ben Plotke (Mar 12 2024 at 13:14):

Anton said:

Hi Ben Plotke,
It needs to be Ok instead of OK

:face_palm: :laughing: :sob:

view this post on Zulip Ben Plotke (Mar 12 2024 at 13:14):

Anton said:

I've seen several similar issues over the last couple of days, what do you think of a tool like this to prevent them?

I'd use it

view this post on Zulip Ben Plotke (Mar 12 2024 at 13:15):

Thanks!


Last updated: Jul 06 2025 at 12:14 UTC