Modules and Namespaces

Modules are compiled to static classes. Files can start with module MyApp.Utils (a top-level module) instead of namespace.

Access control: private, internal, public (the default). Signature files (.fsi) define a module's public surface.

// Geometry.fs
namespace MyApp.Geometry

type Point = { X: float; Y: float }

module Point =                              // a module with the same name as a type (companion module)
    let origin = { X = 0.0; Y = 0.0 }
    let distance a b = sqrt ((a.X - b.X) ** 2.0 + (a.Y - b.Y) ** 2.0)

[<RequireQualifiedAccess>]                  // force qualified usage: Shapes.circle
module Shapes =
    let circle r = System.Math.PI * r * r
    let private helper () = "hidden"        // private to the module

[<AutoOpen>]                                // opened automatically when the namespace is opened
module Extensions =
    type System.String with
        member s.IsBlank = System.String.IsNullOrWhiteSpace s
// Program.fs
open MyApp.Geometry

let d = Point.distance Point.origin { X = 3.0; Y = 4.0 }   // 5.0
let a = Shapes.circle 1.0
"  ".IsBlank                                                 // true