Modules and imports

import for stdlib, use for files or directories, and inline module blocks for declaring sub-namespaces.

Summary

Standard library modules require an explicit import math; the semantic analyzer rejects references to stdlib names without a matching import. File imports use use "path/to/file.xs"; the module name defaults to the filename stem. Both forms accept as alias and selective imports with from mod import { name }. Importing a directory with use "dir/" loads all .xs files in it. Inline modules are declared with module Name { ... } and accessed by name.

Canonical

Importing Standard Library Modules

import math
println(math.sqrt(16))           -- 4.0
println(math.PI)                 -- 3.141592653589793

-- with alias
import math as m
println(m.sqrt(16))              -- 4.0

-- selective import
from math import { sqrt, PI }
println(sqrt(25))                -- 5.0
println(PI)                      -- 3.141592653589793

Importing Files

-- use "path" imports a file as a module (namespace derived from filename)
use "utils.xs"
println(utils.helper())

-- with alias
use "utils.xs" as u
println(u.helper())

-- selective import
use "utils.xs" { helper, VERSION }
println(helper())

For directories, use "dir/" imports all .xs files in the directory.

Declaring Modules Inline

module Utils {
    fn double(x) { return x * 2 }
    fn triple(x) { return x * 3 }
}
println(Utils.double(5))         -- 10
println(Utils.triple(4))         -- 12