This library is in flux. APIs may change without notice. Generated from source on 1/28/2026.

Control

Provides ~if for zero-overhead conditional branching

control.kz

Koru Standard Library: Control Flow Provides ~if for zero-overhead conditional branching ARCHITECTURE: ~if is a pure comptime transform that generates inline Zig code. - ~if(expr) | then |> ... | else |> ... becomes a literal if/else statement - No function call overhead, full access to outer scope - Uses template_utils.lookupTemplate + interpolate for metaprogramming!
// Transform: generates ConditionalNode for if/else (zero overhead)
~[keyword|comptime|transform]pub event if {
    expr: Expression,
    invocation: *const Invocation,  // The specific invocation being transformed
    item: *const Item,
    program: *const Program,
    allocator: std.mem.Allocator
}
| transformed { program: *const Program }
// ============================================================================
// FOR - Iteration over slices/arrays (ZERO OVERHEAD)
// ============================================================================
// 
// Syntax: ~for(iterable_expr)
//         | each item |> process(value: item)
//         | done |> finalize()
// 
// ARCHITECTURE:
// - ~for is a comptime transform that creates a ForeachNode
// - The existing emitter handles ForeachNode emission
// - Zero-overhead: becomes a literal Zig for loop
// 
// Transform: creates ForeachNode for iteration
~[keyword|comptime|transform]pub event for {
    expr: Expression,
    invocation: *const Invocation,  // The specific invocation being transformed
    item: *const Item,
    program: *const Program,
    allocator: std.mem.Allocator
}
| transformed { program: *const Program }
// ============================================================================
// CAPTURE - Accumulation with pure semantics (ZERO OVERHEAD)
// ============================================================================
// 
// Syntax: ~capture(init: { total: 0 })
//         | as c |>
//           for(items)
//           | each i |> captured { total: c.total + i.item }
//         | captured final |> result { sum: final.total }
// 
// ARCHITECTURE:
// - ~capture is a comptime transform that rewrites to ~capture.impl
// - ~capture.impl declares the user-facing branches for shape checking
// - `captured { ... }` branch constructors become assignments
// - inline_body provides zero-overhead code (no handler call)
// 
// SEMANTICS:
// - Pure from Koru's perspective (captured computes NEXT value)
// - Efficient mutation in generated Zig
// - Decomposes fold while preserving purity guarantees
// 
// Transform: creates CaptureNode for accumulation
// Note: First parameter must be named 'expr' for transform_pass_runner compatibility
~[keyword|comptime|transform]pub event capture {
    expr: Expression,
    invocation: *const Invocation,
    item: *const Item,
    program: *const Program,
    allocator: std.mem.Allocator
}
| transformed { program: *const Program }
// ============================================================================
// CONST - Immutable bindings for pipelines
// ============================================================================
// ~const({ threshold: 5 }) | as cfg |> ...use cfg.threshold...
// 
// Much simpler than capture:
// - No mutation, so no `captured` branches to transform
// - No runtime struct type conversion needed
// - Just binds constants and flows through
// 
// Transform: uses preamble_code for the binding, keeps continuations as AST
~[keyword|comptime|transform]pub event const {
    expr: Expression,
    invocation: *const Invocation,
    item: *const Item,
    program: *const Program,
    allocator: std.mem.Allocator
}
| transformed { program: *const Program }