ocamlocamlbuildocaml-batteries

Don't understand symbols in OCaml


I'm learning OCaml these days by some basic material and a project written in OCaml. But I don't understand some symbols in OCaml. For example:

open Batteries

type char_token = [ 
      | `Char of int
      | `Escape of char list
      ]

what's these things with symbol ` mean?
And also other symbols are hard for me to understand:

I can't find anything in the OCaml Manual. Can somebody explain more details about the symbols above? Or just recommend some material to me ?


Solution

  • `Foo and [> are polymorphic variants (http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual006.html#toc36). They are probably not worth it for a beginner, but you could look at one of my old answers (Extending an existing type in OCaml) to see how to use them.

    _ is a pattern that matches anything:

    let head l = match l with
    | x :: _ -> x
    | _ -> failwith "empty list"
    

    Both _ there are used to say to the compiler "something I don't care about".