Checking
(123 : int);;
- : int = 33
(33 : bool);;
Error: This expression has type int but an expression was expected of type bool
int
- Range from to
- one bit is used at runtime (Garbage Collection) to distinguish int from pointers
- Tagged pointer representation
- integer - Why is an int in OCaml only 31 bits? - Stack Overflow
- OCaml library : Int64
- GitHub - ocaml/Zarith: The Zarith library implements arithmetic and logical operations over arbitrary-precision integers and rational numbers. The implementation, based on GMP, is very efficient.
float
int_of_float
float_of_int
char
char_of_int
int_of_char
string
- concatenation:
^
"abc".[0]
type variable
- or generic/polymorphic
'a
: unknown type
type
variant
type t1 = C | D
type t2 = D | E
let x = D
type t = C1 [of t1] | ... | Cn [of tn]
type node = {value : int; next : mylist}
and mylist = Nil | Node of node
type 'a mylist = Nil | Cons of 'a * 'a mylist
let rec length : 'a mylist -> int = function
| Nil -> 0
| Cons (_, t) -> 1 + length t
let empty : 'a mylist -> bool = function
| Nil -> true
| Cons _ -> false
Provide a type-safe way of doing something that might before have seemed impossible.
Variant types may mention their own name inside their own body
Polymorphic Variants
- You don’t have declare their type or constructors before using them.
- There is no name for a polymorphic variant type. (So another name for this feature could have been “anonymous variants”.)
- The constructors of a polymorphic variant start with a backquote character.
let f = function
| 0 -> `Infinity
| 1 -> `Finite 1
| n -> `Finite (-n)