Variables and functions
Variables
Section titled “Variables”x := 42 // inferred: intpi: float = 3.14 // checked against annotationscores: [int] = [1, 2, 3]:=infers from the right side.: T =verifies and names the fix direction on mismatch.- Statements end at newline (depth 0) or
;. Newlines inside(),[],{}do not end statements. - Assignment
=overwrites. Compound forms evaluate the receiver once:arr[i()] += f()callsi()once, not twice.
x := 10x += 1x *= 3flags := 0flags |= 4Functions
Section titled “Functions”func add(a: int, b: int) -> int { a + b // last expression is the return}
func greet(name: str) { println("Hello, {name}") // no return = unit}
func hello(name: str, greeting: str = "Hello") { println("{greeting}, {name}")}
hello("Ada")hello("Ada", greeting: "Hi")Named args: create_user("Ada", age: 30, active: true).
Cannot chain compound assignment (x += y += z is an error).
Generics
Section titled “Generics”func identity<T>(x: T) -> T { x}
result := identity(42) // T = intType args erase at runtime: generic code runs like hand-written copies.
Closures
Section titled “Closures”double := |x: int| x * 2square := |x: int| { x * x}
nums := [1, 2, 3]doubled := map(nums, |x| x * 2)Closures capture by copy (snapshot). They are values and can be passed to
map / filter / task.spawn.
Method syntax
Section titled “Method syntax”p.dist() is dist(p) — receiver becomes first arg:
struct Point { x: int, y: int }func dist(p: Point) -> int { p.x + p.y }
p := Point{ x: 3, y: 4 }dist(p) // 7p.dist() // 7An impl method on the receiver’s own type wins over a same-named free
function, so w.get() reaches W.get even when a free get(w) exists:
struct W { v: int }func get(w: W) -> str { "free" }impl W { func get(self) -> str { "method" }}
w := W{ v: 1 }w.get() // "method" — the impl methodget(w) // "free" — the free function, still reachable by nameWorks for stdlib too: "hi".to_upper(), [3, 1, 2].sort(), server.route(...).
Pitfalls
Section titled “Pitfalls”- Arrays/structs are values: mutating a parameter never mutates the caller.
+onstr/[T]concatenates;+on mixedint+floatyieldsfloat.- Integer overflow wraps (
9223372036854775807 + 1is min-int). Division by zero errors.
Deep reference: syntax.md sections Declarations, Functions, Expressions.