Skip to main content
Functions in Kairo are declared with the func keyword and can live at module scope as free functions, or inside a type body as instance methods. This page covers every function-related keyword — func, type func, init, async func, and await — as well as the local binding keywords let and var and the return statement.

func — Free Functions and Instance Methods

Use func to declare a named function. A func at module scope is a free function; a func inside an object or value body is an instance method and receives an implicit this reference. Syntax:
  • All parameters are named.
  • In v0.1, parameters are either all positional or all named at the call site — you cannot mix styles.
  • The return type follows : after the parameter list. Omit the return type annotation for functions that return nothing (void).

type func — Type-Owned Static Functions

A type func belongs to the type itself rather than to any instance. It has no this receiver and is called directly on the type name. Syntax:
  • Declared inside a value or object body.
  • Called as TypeName.methodName(...).
  • Cannot access instance members because there is no this.
Use type func for factory methods, named constructors, and utility functions that logically belong to a type but do not operate on a specific instance.

init — Object Initialiser

Every object has exactly one init in v0.1. The init sets up the initial state of the object by assigning values to this.member. Syntax:
  • Always called with named arguments at the call site: TypeName(param: value).
  • The init body may only contain assignments to this members and calls to other initialisation helpers.
  • Only one init per object is allowed in v0.1.
Named-argument call syntax at the init call site is mandatory. Positional construction (e.g., Invoice("INV-001", 299.99)) is not valid.

async func — Suspendable Functions

Mark a function with async to declare that it may suspend while waiting for an asynchronous result. An async func can contain await expressions. Syntax:
  • An async func must be called with await at the call site.
  • Calling an async func without await is a compile-time error.

await — Explicit Suspension Point

Use await to suspend execution until an asynchronous operation completes. Every suspension is explicit — Kairo does not automatically promote synchronous calls into async contexts. Syntax:
Rules:
  • await may only appear inside an async func.
  • await does not unwrap a Result — it only suspends the current function until the awaited operation resolves.
  • If the awaited function returns Result<T, E>, you still need to inspect .isSuccess or pattern-match the result yourself.
await does not unwrap Result. After awaiting, check .isSuccess or .isFailure before accessing .value or .error. Accessing the wrong side is a fault.

let — Immutable Local Binding

Use let to declare an immutable local variable. Once bound, the value cannot be reassigned.
  • The type annotation is optional when the compiler can infer it from a simple literal or obvious expression.
  • Attempting to reassign a let binding is a compile-time error.

var — Mutable Local Binding

Use var to declare a local variable whose value can be reassigned. An explicit type annotation is required in v0.1.
Local var bindings always require an explicit type in v0.1. Type inference for mutable locals is planned for a future release.

return Statement

Use return to exit a function and optionally produce a value. In a void function, a bare return exits early. In a function with a return type, return must be followed by an expression of the declared type.

Complete Example

The following module demonstrates free functions, instance methods, a type func, and an init together.