Modules

Organize code with Kode's module system for better reusability.

Creating Modules

Create a module file math.kode:

kode
// math.kode
export fn add(a: int, b: int) -> int {
    return a + b
}

export fn multiply(a: int, b: int) -> int {
    return a * b
}

export const PI = 3.14159

Importing Modules

kode
// main.kode
import math from "./math.kode"

print(math.add(2, 3))        // 5
print(math.multiply(4, 5))   // 20
print(math.PI)               // 3.14159

Named Imports

kode
import { add, multiply, PI } from "./math.kode"

print(add(2, 3))        // 5
print(multiply(4, 5))   // 20
print(PI)               // 3.14159

Namespace Imports

kode
import * as mathUtils from "./math.kode"

print(mathUtils.add(10, 20))
print(mathUtils.PI)

Module Structure

Organize your project with modules:

bash
project/
├── main.kode
├── utils/
│   ├── math.kode
│   ├── string.kode
│   └── array.kode
└── lib/
    └── helpers.kode

Import from subdirectories:

kode
import { add } from "./utils/math.kode"
import { capitalize } from "./utils/string.kode"