Patterns & Common Errors
Main Function Template
function main() returns ExitCode // program logic return 0end 'main'Loops with Break
var i = 0while true 'forever' if i >= 10 'done' break end 'done' print("{i}") i = i + 1end 'forever'Array Iteration
var numbers = [1, 2, 3, 4, 5]for i in numbers 'iter' var num = try numbers.get(i) otherwise 0 print("{num}")end 'iter'Factorial Example
function factorial(n int) returns int if n <= 1 'base' return 1 end 'base' return n * factorial(n: n - 1)end 'factorial'
function main() returns ExitCode var result = factorial(n: 5) print("{result}") // 120 return 0end 'main'Compile-Time & Runtime Errors
Compile-Time Errors
Type Errors
var x = 5 + "string" // ERROR: Type mismatchMissing Return
function test() returns int var x = 5 // ERROR: Missing return statementend 'test'Immutable Assignment
let x = 5x = 10 // ERROR: Cannot assign to immutable variableSelf-Assignment
var x = 5x = x // ERROR: self-assignment has no effectBorrow Conflict
var arr = ["hello"]var s = try arr.get(0) otherwise ""arr.push("world") // ERROR E3070: cannot mutate 'arr' while borrowed by 's'Var Never Reassigned
var x = 10return x // ERROR E3077: variable 'x' is never reassigned; use 'let' instead of 'var'Var From Immutable
let a = Point.create(x: 1, y: 2)var b = a // ERROR E3078: cannot assign immutable variable 'a' to mutable binding 'b'Useless Discard
_ = 42 // ERROR: discarding a non-call expression has no effectUnknown Keyword
functon test() // ERROR: Unknown keyword 'functon'Mismatched Block Identifiers
if x > 0 'check' print("{x}")end 'wrong' // ERROR: Expected 'check', got 'wrong'Empty Block
if x > 0 'check'end 'check' // ERROR E3082: empty blockEvery if, else, while, for, and try...otherwise block must contain at least one statement. Comment-only blocks are also considered empty since comments are not statements.
Runtime Behavior
Caught at runtime (clean panic, exit 1)
CPU faults — division (or modulo) by zero, null pointer dereference, and stack overflow — are caught by the runtime safety handler, which writes a panic: ... line to stderr and exits with status 1.
Undefined Behavior (no error)
- Array out-of-bounds access
- Wrap-around on regular signed/unsigned integer arithmetic
Best Practices for AI Agents
-
Always match block identifiers:
if x > 0 'check'must end withend 'check' -
Initialize all variables: No uninitialized variables allowed
-
Use clear initializers: Type is always inferred from the value
var count = 0 // Type inferred as int -
Return from all code paths:
function test(x int) returns intif x > 0 'pos'return 1end 'pos'return -1 // Don't forget thisend 'test' -
Remember int/float distinction:
var x = 5 // intvar y = 5.0 // float (note decimal point) -
Prefer
letfor immutability:let pi = 3.14159 // Prevents accidental modification -
Export only necessary functions:
export returns int // Public APIreturns int // Private helper -
Handle array access errors:
// Use otherwise for safe access with defaultvar val = try arr.get(index) otherwise 0// Or check bounds firstif index < arr.count() 'safe'var val = try arr.get(index) otherwise 0end 'safe' -
Use meaningful block identifiers:
while not done 'process' // 'process' describes the loop// ...end 'process' -
First argument positional, rest named:
greet("Alice") // Single param is positionalconnect("localhost", port: 8080) // First positional, rest namedmove(start, end: end) // First positional, rest named -
Parameters with defaults can be omitted:
function greet(name String, title String = "Mr.")greet("Smith") // Uses defaultgreet("Smith", title: "Dr.") // Override default -
Use
try otherwisefor error handling:let value = try mayFail() otherwise 42 // Default value on errortry cleanup() otherwise ignore // Ignore errors in cleanup// Use block handler for complex error handlingtry loadData() otherwise 'err'logError("Failed to load data")useDefaults()end 'err' -
Propagate errors with
tryin throwing functions:function process() returns Result throws WorkflowErrorlet data = try loadData() // Propagates error to callerreturn transform(data)end 'process'