Stream: beginners

Topic: Nested Tag Pattern matching


view this post on Zulip Ian McLerran (Jan 11 2024 at 15:56):

I'm trying to pattern match on a a type aliased tag union, where one tag in the union may have a payload, and one may not.
What is the correct way to do this? This is my current implementation, but I receive TYPE MISMATCH: this pattern is being used in an unexpected way errors when I try to run the code.

Node a : [Val a, Null]

hasLhs : List (Node a), Nat -> Bool
hasLhs = \tree, node ->
    res = List.get tree (node * 2 + 1)
    when res is
        Ok Val _ -> Bool.true
        Ok Null -> Bool.false
        Err OutOfBounds -> Bool.false

Error:

── TYPE MISMATCH ──────────────────────────────────────── BinaryTreeOnList.roc ─

This pattern is being used in an unexpected way:

12│          Ok Null -> Bool.false
             ^^^^^^^

It is a Ok tag of type:

    [Ok …]

But it needs to match:

    [Ok … *]

view this post on Zulip Brendan Hansknecht (Jan 11 2024 at 16:00):

Try Ok (Val _)

view this post on Zulip Brendan Hansknecht (Jan 11 2024 at 16:01):

Man is that an error message we should fix. We know the type of res so we really should say the first branch is the wrong type, not the second.

view this post on Zulip Brendan Hansknecht (Jan 11 2024 at 16:01):

But yeah, either way that message should probably note parens might be needed.

view this post on Zulip Ian McLerran (Jan 11 2024 at 16:03):

Nice, that did it. Yeah, error message was not very helpful in trouble shooting that.

view this post on Zulip Richard Feldman (Jan 11 2024 at 16:14):

wow yeah I've never seen that one before :sweat_smile:


Last updated: Jul 05 2025 at 12:14 UTC