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

标签 syntax ocaml

当我在 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/

相关文章:

Perl 在一行中拆分和弹出

c# - 将字符串拆分成行的最佳方法

compiler-errors - Ocaml-匹配两个列表

F# 更改为 OCaml

command-line - 如何在 OCaml 中使用本地程序执行命令?

python - Python中有 'cross-referencing'生成器吗?

ruby - OpsWorks Chef : Cannot compile yum cookbook due to old Ruby (1. 8.7)

SQL Server 中的 MySQL INDEX() 语法等效

ocaml - 如何从 mli 文件自动生成 stub ?

ocaml - 找不到记录字段注释