shooqie
shooqie

Reputation: 1022

"Variable ... must occur on both sides of this | pattern"

As I was typing up some code in OCaml, I wanted to match two cases at once (since the function I'm writing is commutative):

type something = 
  | Two of int * int
  | One of int

let my_function p q =
  match p, q with
    | Two (_, _) as two, One (x)
    | One (x), Two (_, _) as two -> (* some value *)
    | _ -> (* some other value *)
;;

I'm getting the following error:

Error: Variable two must occur on both sides of this | pattern

The problem doesn't occur when I remove the as statement, but I need it for the logic purposes. Why can't I do it like this? Will I have to resort to rewriting the logic twice?

Upvotes: 3

Views: 291

Answers (1)

kne
kne

Reputation: 1418

as has lower precedence than ,. Hence you should put parenthesis around Two (_,_) as two.

Upvotes: 3

Related Questions