当我在OCaml中输入一些代码时,我想同时匹配两个案例(因为我写的函数是可交换的):
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 *)
;;
我收到以下错误:
Error: Variable two must occur on both sides of this | pattern
当我删除as
语句时,问题不会发生,但出于逻辑目的我需要它。为什么我不能这样做呢?我是否不得不两次重写逻辑?
答案 0 :(得分:3)
as
的优先级低于,
。因此,您应该在Two (_,_) as two
附近加上括号。