#Syntax and Bindings
Rig uses parenthesized expressions with positional, named, and typed slots. Function calls, bindings, control flow, and type construction share this notation.
#Expressions and Slots
In (add 20 22), the first expression selects the operation and the following expressions supply its arguments. Calls can nest: (mul (add 2 3) 4).
Some forms give slots names. In (if ready then=1 else=0), the condition is positional, while then= and else= identify the branches. A typed slot such as answer:Int declares a name and its type.
These are distinct uses of the punctuation:
answer:Int declares the binding's type. answer=... supplies its initializer; it is not assignment to an existing variable. Here, add returns the Int value 42.
Whitespace separates slots. Use line breaks and indentation to organize expressions. A # comment continues to the end of its line. Inside a raw block string, indentation also determines the content.
#Defining Values
define introduces module-level bindings. Several bindings can share one form:
Each initializer may use preceding declarations. A binding can hold any expression of its declared type, including a function returned by another function or selected by a conditional.
Bindings are immutable. Mutation is an operation on an explicit reference or mutable collection, not on the name itself. See Types and Values.
#Local Bindings
let uses the same declaration-and-initializer pairs, followed by an in= expression:
The local names are visible to later initializers and the body. The value of let is the value of its in= expression. Here, (adjust 12) returns 42.
Local bindings initialize each time execution reaches them at their stage. Module-level bindings initialize once per owning evaluation at their stage. A local binding shadows an outer name in its scope.
#Contextual Typing
Declared types do more than check an already-chosen literal type. They supply context:
The literal 42 becomes Nat8, the quoted literal becomes Text, and the list elements become Int. A uniquely selected function can supply the same context through its parameter and result types.
Without a selected context, integer literals default to Int, floating-point literals to Float, and quoted literals to fixed-capacity String. Record, Variant, Sequence, and List construction need an expected composite type. Rig does not infer a general binding type from an unannotated initializer.
Context is not an implicit conversion. An existing Int value cannot be passed where Nat8 is required without an explicit conversion. Functions and Control Flow explains how context interacts with overload selection.
#Order and Initialization
Ordinary declarations and eager initializers are source-ordered. Direct module-level function bodies can also refer to sibling functions in the same define; this does not extend their annotations' scope. See Recursion. Guarded recursive Type groups have their own construction rule.
Initialization is eager, including for unused bindings. It follows stage order and module dependencies, then source order within the executing stage. Knowing a binding's type does not mean its value has initialized; an actual read before initialization traps rather than producing a default value.
Module-level initialization cannot perform asynchronous host calls or endpoint operations that require suspension. Put asynchronous setup inside an exported function. Calling through a helper does not bypass this restriction. Ordinary local initialization inside such a function follows the function's execution context.
Types determine stages, so one define or let may contain bindings from different stages. Staging explains how those bindings are evaluated.