当我在 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
语句时,问题没有发生,但出于逻辑目的我需要它。为什么我不能这样做?我是否必须重写逻辑两次?
最佳答案
as
的优先级低于 ,
。因此,您应该在 Two (_,_) as two
两边加上括号。
关于syntax - "Variable ... must occur on both sides of this | pattern",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329605/