TExcelTAD
Home › OCaml › OCaml Basic Types

OCaml Basic Types

OCaml has a small set of built-in basic types. Every value has exactly one type, and the compiler never converts between types silently.

Type Example values Notes
int 0, 42, -7, 1_000_000, 0xff, 0b1010 63-bit on 64-bit machines
float 3.14, 1., 2e10, infinity, nan 64-bit IEEE double
bool true, false
char 'a', '\n', '\065' one byte
string "hello", "" immutable byte sequence
unit () "no interesting value"

int

Integers are 63 bits wide on 64-bit systems (one bit is used by the garbage collector to tell numbers from pointers). Underscores can make long literals readable:

ocaml · try it yourself
let population = 8_100_000_000
let mask = 0xff
let flags = 0b1010

let () =
  Printf.printf "%d %d %d\n" population mask flags;
  Printf.printf "max_int = %d\n" max_int;
  Printf.printf "overflow: %d\n" (max_int + 1);
  Printf.printf "17 / 5 = %d, 17 mod 5 = %d\n" (17 / 5) (17 mod 5)
8100000000 255 10
max_int = 4611686018427387903
overflow: -4611686018427387904
17 / 5 = 3, 17 mod 5 = 2

Integer arithmetic wraps around silently on overflow, and / on ints truncates towards zero. For exact big numbers you would use the zarith library (an opam package).

float

Float literals must contain a . or an exponent: 2.0 or 2. is a float, 2 is an int. Float operators end in a dot:

ocaml · try it yourself
let () =
  let price = 19.99 in
  let quantity = 3. in
  Printf.printf "%f\n" (price *. quantity);
  Printf.printf "%.2f\n" (price *. quantity);
  Printf.printf "%g\n" (1.0 /. 3.0);
  Printf.printf "%s\n" (string_of_float 2.0);
  Printf.printf "%f %f\n" (1.0 /. 0.0) (sqrt (-1.0));
  Printf.printf "%.1f\n" (2.0 ** 10.0)
59.970000
59.97
0.333333
2.
inf -nan
1024.0

Notice that string_of_float 2.0 gives "2." (OCaml's own literal syntax) and that dividing by zero gives inf rather than an error. ** is float exponentiation.

bool

bool has two values, true and false. Comparisons produce bools, and &&, || and not combine them:

ocaml · try it yourself
let () =
  let age = 20 in
  let has_ticket = true in
  let can_enter = age >= 18 && has_ticket in
  Printf.printf "%b %b %b\n" can_enter (not can_enter) (age < 13 || age > 65)
true false false

char and string

A char is a single byte in single quotes. A string is a sequence of bytes in double quotes. Strings are immutable:

ocaml · try it yourself
let () =
  let initial = 'O' in
  let name = "OCaml" in
  Printf.printf "%c\n" initial;
  Printf.printf "%d\n" (Char.code initial);
  Printf.printf "%c\n" (Char.chr 97);
  Printf.printf "%d\n" (String.length name);
  Printf.printf "%c\n" name.[1];
  Printf.printf "%s\n" (name ^ " " ^ String.make 3 '!')
O
79
a
5
C
OCaml !!!

name.[1] reads the character at index 1 (indices start at 0). Because strings are bytes, String.length "é" is 2: a non-ASCII character in UTF-8 takes several bytes.

unit

unit has exactly one value, written (). Functions that only do something (like printing) return unit, and functions that need no input take it:

ocaml
# print_endline;;
- : string -> unit = <fun>
# ();;
- : unit = ()

No implicit conversions

Mixing types is always an error. 1 + 2.5 does not compile, and the compiler message points at the exact spot:

ocaml · try it yourself
let () = Printf.printf "%d\n" (1 + 2.5)
File "<source>", line 1, characters 35-38:
1 | let () = Printf.printf "%d\n" (1 + 2.5)
                                       ^^^
Error: The constant 2.5 has type float but an expression was expected of type
         int

You must convert explicitly instead:

ocaml · try it yourself
let () = Printf.printf "%f\n" (float_of_int 1 +. 2.5)
3.500000

The next two lessons cover the operators and the conversion functions in detail.

Test yourself

Which of these is a float literal?

  1. 3
  2. 3.
  3. '3'
  4. "3"

What is max_int + 1 in OCaml?

  1. An exception
  2. min_int (it wraps around)
  3. A float
  4. max_int

What does "hello".[0] evaluate to?

  1. "h"
  2. 'h'
  3. 104
  4. An error