Jackson Tale
Jackson Tale

Reputation: 25812

What are "`" in OCaml?

type t = {
      dir : [ `Buy | `Sell ];
      quantity : int;
      price : float;
      mutable cancelled : bool;
    }

There is a ` before Buy and Sell, what does that mean?

Also what are the type [ | ]?

Upvotes: 5

Views: 129

Answers (1)

seanmcl
seanmcl

Reputation: 9946

The ` and [] syntax are to define polymorphic variants. They are similar in spirit to an inline variant definition.

http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual006.html#toc36

In your case, dir can take the value `Buy or `Sell, and pattern matching works accordingly:

let x = { dir = `Buy, quantity = 5, price = 1.0, cancelled = true }

match x.dir with 
| `Buy -> 1
| `Sell -> 2

Upvotes: 7

Related Questions