#Functions and Control Flow

Functions are ordinary values in Rig. They can be bound, passed, returned, or selected by expressions without changing how they are called.

#Function Types and Application

The last type in a Fn is the result; the preceding types are parameters:

(define sum:(Fn Int Int Int) sum=(fn left:Int right:Int do=(add left right)))

(sum 20 22) returns 42. The names belong to the function's parameters, not its function type. A zero-argument function has a type such as (Fn Int) and is called as (answer).

For a function-producing expression, make the two calls explicit: ((make_offset 2) 40) first obtains a function, then calls it.

#Closures

A returned function can capture values from its defining invocation:

(define make_offset:(Fn Int (Fn Int Int)) make_offset=(fn offset:Int do=(fn value:Int do=(add value offset))))

Each call to make_offset produces a function with its own copied offset. It remains usable after that invocation returns. Capturing a reference instead copies its handle, so later calls observe shared mutation and ordinary handle invalidation.

Fn is the fixed-size Fn64 representation: a callable identity and seven inline capture slots. Capturing an Int or reference uses one slot. A full String or another full function value uses eight and cannot fit directly. An explicit Box can provide indirection when that allocation is intended. Rig does not allocate a hidden closure environment to exceed the budget.

Module-level dependencies are different: functions retain links to their defining module's static bindings. Those links consume no capture slots, regardless of the referenced value's type. Reading through a static link still requires that the binding has initialized.

#Conditional Expressions

if is an expression. Its condition is a Bool. The branches that return a value must agree on the result type:

(define maximum:(Fn Int Int Int) maximum=(fn left:Int right:Int do=(if (lt left right) then=right else=left)))

Only the selected branch executes at the active stage. Higher-stage work follows stage order, not a lower-stage condition.

and and or short-circuit Boolean expressions; not negates a Bool. Numeric comparisons use eq and lt. More elaborate predicates can be ordinary functions built from these operations.

Use Variant matching when control flow depends on a case and its payload, or select to pass named Record fields into an inline function.

#Sequencing and Loops

do evaluates its expressions in order and returns the last value. A while repeatedly checks a Bool condition, executes its do= body while true, and returns Null:

(define sum_below:(Fn Int Int) sum_below=(fn limit:Int do=(let index:(Mutable Int) index=(mutable 0) total:(Mutable Int) total=(mutable 0) in=(do (while (lt (get index) limit) do=(do (set total (add (get total) (get index))) (set index (add (get index) 1)))) (get total)))))

The loop state is explicit. (sum_below 5) returns 10, and each call creates fresh local cells. The immutable bindings index and total continue to name the same cells throughout the call.

#Recursion

Direct module-level fn bindings in one define can refer to one another:

(define even:(Fn Int Bool) even=(fn value:Int do=(if (eq value 0) then=true else=(odd (sub value 1)))) odd:(Fn Int Bool) odd=(fn value:Int do=(if (eq value 0) then=false else=(even (sub value 1)))))

This pair is intended for nonnegative inputs. Its bodies share recursive visibility, but annotations and eager initializers remain source-ordered. The group's function values are constructed together at its first direct function declaration; constructing them does not execute their bodies.

Function bindings in separate define forms, and function bindings in let, remain sequential. A function produced by a factory call or conditional is an ordinary initialized value, not a direct recursive declaration. Rig does not build hidden cyclic environments for local recursive closures or promise tail-call optimization.

#Overloading

Different types can share a name:

(define double:(Fn Int Int) double=(fn value:Int do=(add value value)) double:(Fn Float Float) double=(fn value:Float do=(add value value)))

Calls select by exact argument types, not by the expected return type. There are no implicit numeric coercions. A bare overloaded value needs enough context to select one exact function type.

A uniquely selected callable can provide literal context. Competing overloads do not try a literal under every possible parameter type: an unconstrained integer defaults to Int and a quoted literal to String before matching. Use an annotated value or explicit conversion when another type is intended.

Compiler operations and Record field projections participate in the same selection rules as source functions. They do not silently take precedence over an equally applicable source binding. Public Component exports must have unique external names; source-module overloads remain available internally.

#Failure

(trap) is an explicit unrecoverable failure and returns no value. Violated primitive preconditions, stale handles, host failures, or execution limits may also trap. A trap aborts the Component instance, not just the current function.

Represent recoverable failures as ordinary Variants and match their cases. assert is for observed test assertions, not a runtime validation or recovery mechanism.