OCaml Tutorial: Record Type

By Xah Lee. Date: .


A Record datatype is similar to fixed table lookup structure. It's a collection of Key and Value pairs.

(* defines a “record” datatype *)

(* define a type “person” that is a particular subset of
   a compound type called Record *)
type person = {mutable name:string; mutable age:int };;

let mj = {name="Mary Jane"; age=19 };;

(* extracting a field *)
print_string mj.name;                   (* prints “Mary Jane” *)

(* changing a field's value *)
mj.name <- "Mary Johnson";

print_string mj.name;                   (* prints “Mary Johnson” *)

Ask me question on patreon