Skip to content

Modules and Imports

NG uses a file-based module system. Every .ng file is a module; the module name is the file name.

Imports

ng
import prelude;            // wildcard: imports prelude's exported surface
import string (trim, split);  // selective: only these names

Imports are transitive: an imported module's own wildcard imports re-export through it, so import prelude; also brings the string/io utilities the prelude re-exports.

Exports

export marks items importable from other modules; non-exported functions stay private to the module:

ng
export fun greeting() -> string {
    return "hello from module";
}

fun helper() -> i64 {   // module-private
    return 1;
}

The standard library

lib/std/*.ng is the stdlib, importable by name from any module (the loader searches lib/std next to the importing file and upward):

ModuleSurface
preludeprint/assert, not, and re-exports of io/string
ioreadLine, readFile, writeFile, currentExecutablePath
systemsystem(command), systemOutput(command) — process/system command bindings (import system;)
stringlength, charAt, substring, trim, split, join, contains, replace, startsWith, endsWith, toUpper, toLower, regexMatch
seqlen, sum, arrayContains, reverse
listrecursive-enum List<T> with length/get/contains and builders listFrom/pushFront/append/reverseList
memoryGC-free native handles: box, read, write, outstanding with impl Drop release
imguithe Dear ImGui binding (see ImGui Integration)

import prelude; is the usual first line of a program.

Example

example/modules/hello.ng:

ng
export fun greeting() -> string {
    return "hello from module";
}

example/modules/imports_main.ng:

ng
import hello;

fun main() -> unit {
    print(hello.greeting());
}
bash
./build/ngi example/modules/imports_main.ng

Next: Generics.

Made with ❤️ by the NG community.