type t = {
dir : [ `Buy | `Sell ];
quantity : int;
price : float;
mutable cancelled : bool;
}
Run Code Online (Sandbox Code Playgroud)
在购买和销售之前有一个`,这是什么意思?
还有什么类型[ | ]?
`和[]语法用于定义多态变体.它们在精神上与内联变体定义类似.
http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual006.html#toc36
在你的情况下,dir可以取值`Buy或`Sell,并且模式匹配相应地工作:
let x = { dir = `Buy, quantity = 5, price = 1.0, cancelled = true }
match x.dir with
| `Buy -> 1
| `Sell -> 2
Run Code Online (Sandbox Code Playgroud)