Question

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 ?

Was it helpful?

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".

OTHER TIPS

Identifiers that begin with a ` are polymorphic variants. They are structurally typed variants, roughly speaking.

|> is an infix operator: you are probably thinking of the one defined in Batteries, which is function application (x |> f meaning f x).

_ is a special symbol in match patterns meaning "match anything without making a binding".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top