Skip to main content
Kira

Modules

Declaring a Module

Each file can declare what module it belongs to:

// src/math/vector.ki
module math.vector

pub type Vec2 = {
    x: f64,
    y: f64
}

pub fn add(a: Vec2, b: Vec2) -> Vec2 {
    return Vec2 { x: a.x + b.x, y: a.y + b.y }
}

// Private function (no pub keyword)
fn helper(x: f64) -> f64 {
    return x * x
}

Importing

// Import entire module
import std.list

// Import specific items
import std.list.{ map, filter, fold }

// Import with alias
import math.vector.{ Vec2, add as vec_add }

Visibility

  • pub makes an item public (accessible from other modules)
  • Without pub, items are private to the module
pub type PublicType = { ... }   // Accessible everywhere
type PrivateType = { ... }       // Only in this module

pub fn public_function() -> void { ... }
fn private_function() -> void { ... }

Complete Example

File: src/math/vector.ki

module math.vector

pub type Vec2 = {
    x: f64,
    y: f64
}

pub fn add(a: Vec2, b: Vec2) -> Vec2 {
    return Vec2 { x: a.x + b.x, y: a.y + b.y }
}

pub fn dot(a: Vec2, b: Vec2) -> f64 {
    return a.x * b.x + a.y * b.y
}

Using the module:

import math.vector.{ Vec2, add, dot }

effect fn main() -> void {
    let v1: Vec2 = Vec2 { x: 1.0, y: 2.0 }
    let v2: Vec2 = Vec2 { x: 3.0, y: 4.0 }
    let sum: Vec2 = add(v1, v2)
    std.io.println("Sum: (" + to_string(sum.x) + ", " + to_string(sum.y) + ")")
}