Modules
You can use export and import to create modules in your project.
export fn add(a, b) a + b
export fn subtract(a, b) a - bHere, we look for an add function from a math.ngn file within the same directory that your main.ngn file is in.
import { add } from "math.ngn"
fn main() { print(add(21, 3)) // 24}Relative path imports
Section titled “Relative path imports”import { add } from "./lib/math.ngn"Aliased imports
Section titled “Aliased imports”import { add as adder } from "math.ngn"Module imports
Section titled “Module imports”import * as Math from "math.ngn"
print(Math.subtract(10, 2)) // 5Default export
Section titled “Default export”fn add(a, b) a + b
export default addimport add from "math.ngn"Exporting types and models
Section titled “Exporting types and models”export type UserId = i64
export model User { id: UserId, name: string}Exporting roles and enums
Section titled “Exporting roles and enums”export role Named { fn name(): string}
export enum Status { Active, Disabled, Error(string),}import { Named, Status } from "domain.ngn"
fn describe(status: Status): string { match status { Status::Active => "active" Status::Disabled => "disabled" Status::Error(message) => "error: ${message}" }}import { User, UserId } from "types.ngn"
fn format_id(id: UserId): string { return "id=${id}"}
fn main() { const user = User { id: 7, name: "Ari" } print(format_id(user.id))}