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 … *]
Try Ok (Val _)
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.
But yeah, either way that message should probably note parens might be needed.
Nice, that did it. Yeah, error message was not very helpful in trouble shooting that.
wow yeah I've never seen that one before :sweat_smile:
Last updated: Jul 05 2025 at 12:14 UTC