Equality constraint in pattern match

Hello there,
I’d like to ask whether it is somehow possible to write pattern match (in case in this example) with multiple occurrences of single variable “placeholder”. By this I would be able to “constraint” cased expression even more…

Like so:

case ( cookie.selectedCategoryId, c.id ) of
    ( Just selectedId, selectedId ) ->
        "It equals!"
            
    (_, _ ) ->
        "It does not equal or 1st item of tuple is Noothing"

In this example I would like to check that cookie.selectedId is Just a and a equals to c.id

But it produces following error:

-- NAME CLASH --------------- C:\....\Main.elm

This `case` pattern has multiple `selectedId` variables.

651|                     ( Just selectedId, selectedId ) ->
                                            ^^^^^^^^^^
How can I know which one you want? Rename one of them!

Should it be possible to write something like that or shall I figure it out using additional if?

You can’t assign multiple values to the same variable name in a pattern-match that way.

If you need to do this kind of comparison, you can either unwrap first and then compare, or it might make sense to compare two wrapped values like:

if cookie.selectedCategoryId == (Just c.id) then
  "It equals"
else
  "It does not equal or cookie selected category id is Nothing"
1 Like

Oh, I see. Didn’t thought of this kind of comparison… that is more compact solution…
Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.