【发布时间】:2021-12-27 08:44:15
【问题描述】:
我在看这段代码(前两行是上下文的伪代码)
typ = Void | Bool | Int
type bind = typ * string
let check_binds (kind : string) (binds : bind list) =
List.iter (function
(Void, b) -> raise (Failure ("illegal void " ^ kind ^ " " ^ b))
| _ -> ()) binds;
所以我认为正在发生的事情是有一个名为binds 的列表,并且由于iter,在“List.iter”之后的括号内定义的函数被应用于binds 中的每个项目。
但是我对函数本身感到困惑。这是我尝试单独写出函数的尝试
function
(Void, b) -> raise (Failure ("illegal void " ^ kind ^ " " ^ b)
| _ -> ()
_ -> () 是什么意思?
【问题讨论】:
-
看起来
check_binds如果绑定中有任何 Void 会引发错误,否则不会返回任何感兴趣的内容。 -
一些风格注释:这里有一些真正的缩进,你的
check_binds函数可以在没有显式类型注释的情况下推断其参数的类型。您不妨查看OCaml Programming Guidelines。
标签: function syntax compiler-construction ocaml ocamlyacc