typestar

Modules in OCaml

Group values and types; the dot reaches inside.

(* A module groups values and types; the dot reaches inside. *)
module Tide = struct
  type phase = Ebb | Flood

  let describe = function
    | Ebb -> "falling toward the survey window"
    | Flood -> "rising, pack up the quadrats"

  let window_minutes = 90
end

let () =
  print_endline (Tide.describe Tide.Ebb);
  Printf.printf "Window: %d minutes\n" Tide.window_minutes;
  (* open brings a module's names into scope locally. *)
  let open Tide in
  print_endline (describe Flood)

How it works

  1. module Tide = struct ... end bundles a type and functions.
  2. Tide.describe Tide.Ebb qualifies both name and constructor.
  3. let open Tide in unqualifies names for one scope.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
460
Tokens
70
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 2 in Modules, step 22 of 29 in Language basics.

← Previous Next →

Modules in other languages