Keywords · MODULE

MODULE

A MODULE is a named namespace that holds declarations and nests to any depth.

Format

The grammar of a single MODULE line:

MODULE ::= "MODULE" identifier
identifier ::= letter { letter | digit }
A MODULE keyword is followed by the namespace name, with the module's declarations on the indented lines below.

Usage

A MODULE keyword can live inside MODULE, TYPE, and FUNCTION keywords, or at the Root.

Inside a MODULE

A module nested directly inside another, opening a deeper namespace within it.

MODULE Code
  MODULE Language
    MODULE Json
Modules nesting three deep; each opens a namespace within the one above.

The outer module's name prefixes everything the inner one declares, so the innermost is named Code.Language.Json from anywhere else in the program.

Inside a TYPE

A module inside a type acting as a pseudo-module, scoped under the type's dotted name.

TYPE Value
  MODULE Parse
    TYPE Result
A type acting as a pseudo-module, with a nested module under its name.

The module nests beneath the type's dotted name, so the inner Result here is named Value.Parse.Result.

Inside a FUNCTION

A module inside a function acting as a pseudo-module, scoped under the function's dotted name.

FUNCTION Value Parse
  MODULE Helpers
    TYPE Token
A function acting as a pseudo-module, holding a nested module.

The module nests beneath the function's dotted name, prefixing everything it declares.

At the Root

At the program's Root a module opens a root namespace — the outermost scope its members are named under.

MODULE Code
  TYPE Value
    OPTION Text text
A Root module opening a root namespace.

With nothing above it, the module opens the outermost scope, and its name prefixes every member declared inside.

Can Contain

Declarations, in any order — and, as the one exception, NATIVE setup that runs at the module's start. No other running statements appear. Each kind below lives directly inside the module:

Containing a MODULE

A nested module, scoping a deeper namespace within this one; modules nest to any depth.

MODULE Code
  MODULE Language
A module nested inside another, opening the deeper namespace Code.Language.

Containing a FUNCTION

A derived function declared in the module's namespace and called by its qualified name.

MODULE Greeting
  FUNCTION Text Make
    INPUT Text name
    = Text.Append "Hello, " name
A function in the module’s namespace, called as Greeting.Make.

Containing a TYPE

A type belonging to the module — a record of fields or a union of options, with its members and constructor scoped in this namespace.

MODULE Geometry
  TYPE Point
    FIELD Maths.Real x
    FIELD Maths.Real y
A record type belonging to the module, named Geometry.Point.

Containing a LET

A named constant in the module's namespace — a fixed value bound with a constant expression (a literal, or a type constructor applied to literals) and reached through the module's name. Unlike a LET in a body it is a declaration, not running code.

MODULE Maths.Real
  LET Maths.Real Pi = 3.14159
Defining a module-level constant called Maths.Real.Pi.

Containing a NATIVE

A per-target block of native host code that runs once at the module's start, whenever the module is used in a program — the module's setup on that platform. It is the only running code a module holds.

MODULE Analytics
  NATIVE js "window.analytics.setup()"
Per-target host code that runs once at the module’s start.

Targets

The same example, rendered to each target.

JavaScript

// each module becomes a nested namespace object
var Code = {};
Code.Language = {};
Code.Language.Json = {};
// the Code.Language.Json.Value union is attached here

TypeScript

// dotted names are kept as nested namespaces
export namespace Code.Language.Json {
  // the Value union is declared here
}

Dart

// Dart has no nested namespaces, so the path joins with _
// the union type becomes Code_Language_Json_Value

Swift

// nested caseless enums act as namespaces
enum Code { enum Language { enum Json { /* Value */ } } }

Go

// Go has no nesting, so the path joins with _
package code_language_json
// the union type becomes Code_Language_Json_Value

Rust

// nested modules, lowercased to snake_case
pub mod code { pub mod language { pub mod json { /* Value */ } } }

Zig

// nested struct namespaces, snake_case
const code = struct { const language = struct { const json = struct { /* Value */ }; }; };

JSON

["M", "Code", [
  ["M", "Language", [
    ["M", "Json", [
      ["U", "Value", [], [
        ["P", "text", "Text"],
        ["P", "number", "Maths.Real"]
      ]]
    ]]
  ]]
]]

Related

A module's members are declared with FUNCTION and TYPE, and nested MODULEs. Running statements such as STATE live outside a module, in the run body. Back to the language.