Modules¶
Organizing code into reusable modules with import, open, and inline module definitions.
// Inline module definitions
module Math =
let square (x: int) : int = x * x
let cube (x: int) : int = x * x * x
module Utils =
let double (x: int) : int = x * 2
let inc (x: int) : int = x + 1
// Qualified access: Module.function
println (Math.square 5)
println (Math.cube 3)
println (Utils.double 10)
// Chaining calls across modules
println (Utils.inc (Math.square 4))
Key Techniques¶
- Inline modules are defined with
module Name = ...(indentation-based scoping). The module name must be PascalCase. - Qualified access uses
Module.functionsyntax to call functions from a specific module. This avoids name collisions between modules. - File-based modules work the same way: a file named
Math.endobecomes moduleMath. Useimport Mathto load it, then access members withMath.square 5. openbrings all module names into the current scope so you can callsquare 5directly without theMath.prefix.open Math with (square)selectively imports only listed names.- Import-once: Each module is loaded and evaluated exactly once per session, no matter how many times it is imported or opened.
- Shell harmony:
import,open, andmoduleonly act as module keywords when followed by a PascalCase name.import requestsoropen file.txtare still treated as shell commands.