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 }
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
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
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
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
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
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
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
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
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()"
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.