Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "`" in OCaml?

Tags:

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 [ | ]?

like image 213
Jackson Tale Avatar asked Oct 21 '13 15:10

Jackson Tale


Video Answer


1 Answers

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
like image 77
seanmcl Avatar answered Sep 28 '22 16:09

seanmcl