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
module Tide = struct ... endbundles a type and functions.Tide.describe Tide.Ebbqualifies both name and constructor.let open Tide inunqualifies names for one scope.
Keywords and builtins used here
EbbFloodTideendfunctioninletmoduleopenstructtype
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.
Step 1 of 2 in Modules, step 22 of 29 in Language basics.