Skip to content

Modules ​

Modules help organize code into separate files.

Importing Modules ​

Use import to bring definitions from other modules:

kettle
import utils

fn main() -> Unit using file_io
  result = utils.helper(5)
  print(to_string(result))
end fn

Module Structure ​

A Kettle file is a module. The file name (without .ket) is the module name:

project/
  main.ket        -- main module
  utils.ket       -- utils module

Defining a Module ​

Every .ket file defines a module. All top-level definitions are exported:

kettle
-- utils.ket

fn helper(x: Int) -> Int
  x + 1
end fn

type Config = record
  name: String
  value: Int
end record

Using it:

kettle
-- main.ket
import utils

fn main() -> Unit using file_io
  result = utils.helper(5)
  cfg = utils.Config { name: "test", value: 42 }
  print(to_string(result))
end fn

Built-in Functions ​

Core functions like print, map, filter, fold, and length are always available without imports. These are built into the language.

Circular Imports ​

Kettle does not allow circular imports. If module A imports module B, then B cannot import A (directly or indirectly).

Structure your code to avoid cycles:

-- Bad: circular
a.ket imports b.ket
b.ket imports a.ket

-- Good: shared dependency
a.ket imports common.ket
b.ket imports common.ket

Next Steps ​