Module System

Zith's module system controls how code is organised, imported, and exposed across files and packages.

Import Keywords

Keyword Behaviour
import path as name Imports a module; path becomes its namespace, or you access via name.symbol.
from path Injects all public symbols directly into scope (sugar for beginners).
export path Re-exports a dependency; consumers of this module also receive it.
import std/io/console as console;
@console.println("hi");

from std/io/console;
@println("hi");

export std/io/console;

alias vs use

Keyword Purpose Example
alias Create a name alias for a type, namespace, or symbol. alias Vec = std.collections.DynArray;
use Bring a word, context, or operator into the current scope. use DOT = math.vec.dot; / use SQL;
alias Vec   = std.collections.DynArray;
alias print = std.io.console.println;

use math.vec.dot;
use SQL;

Namespace Access & Scope Resolution

Namespaces are accessed with . — e.g. std.io.console.println. The :: operator reaches upward past a shadowed name to the outer scope.

let x = 10;
{
    let x = 20;
    @println(::x);   // 10 -- outer scope
}

Type Constraints vs Union Separators

Construct Separator Semantics
Type constraint or (keyword) Compile-time template restriction. Dispatched at compile time.
Union body , (comma) Runtime tagged union. Variants separated by commas.
// Type constraint — compile-time dispatch
type Number = i32 or f64 or bool;
fn convert<T: Number>(val: T): string { ... }

// Union — runtime tagged
union Number { i32, f64, bool }

Visibility

Modifier Scope
(none) Private — visible only within the declaring file.
pub Public — visible to any importer.
mod Module-local — visible to immediate sibling files in the same directory.
mod(..) Visible to all sub-directories, unlimited depth.
mod(N) Visible to exactly N levels of sub-directories deep.

See also: Spec: Module System


Next: Type System