Skip to content
kata / a language workbench

Modules and errors

The prelude automatically imports Opt, Res, core protocols, and the common collection types. Other standard-library names can be imported explicitly.

import dsa
let values = dsa.Arr[Int].new()
values.push(42)
print(values[0])
import mem.{Ptr, Buf}
print(Ptr)
print(Buf)

import dsa binds a module value, so its exports are accessed with dsa.name. The selective form binds only the requested exports directly.

The built-in module families are core, mem, and dsa. Imports resolve the embedded standard library; they do not currently load arbitrary neighboring .ks files. A general user-module system and package manager are not present.

The mem module exposes low-level memory facilities. The unsafe block gates raw memory intrinsics, but it does not solve the runtime’s outstanding ownership and lifecycle problems.

Opt[T] represents presence or absence:

let names = ["Ada", "Lin"]
match names.get(5) {
Val(name) -> print(name),
Non() -> print("no name at that index"),
}

Use unwrap_or(default) when a default value is appropriate. unwrap() assumes a value is present and stops execution if it is not.

Res[T, E] carries either Val(T) or Err(E).

func divide(a: Int, b: Int): Res[Int, Str] {
if b == 0 {
ret Res[Int, Str].Err("division by zero")
}
ret Res[Int, Str].Val(a / b)
}
match divide(12, 0) {
Val(value) -> print(value),
Err(reason) -> print(reason),
}

The function returns an ordinary enum value in both cases. There is no exception unwinding or try/catch.

Inside a function, postfix ? unwraps a successful value or returns the failure immediately:

func divide(a: Int, b: Int): Res[Int, Str] {
if b == 0 { ret Res[Int, Str].Err("division by zero") }
ret Res[Int, Str].Val(a / b)
}
func halve_quotient(a: Int, b: Int): Res[Int, Str] {
let quotient = divide(a, b)?
ret Res[Int, Str].Val(quotient / 2)
}
print(halve_quotient(20, 2).unwrap_or(0))

The failure value is propagated unchanged. There is no automatic conversion between error types or result instantiations; the enclosing function’s return check still applies. ? also supports Opt and user enums with the supported Val/Err or Val/Non shape.

Postfix ! unwraps success and stops execution on failure. It is an assertion, not recovery. Native runtime errors, including invalid string-to-number conversion, are not turned into Res by either operator.

The result tutorial develops a complete API around this distinction.