Syntax
Declarations
Section titled “Declarations”:= infers the type; : Type = declares it explicitly:
x := 10 // inferred: intname := "ZZ" // inferred: strpi := 3.14 // inferred: floatcount: int = 0 // explicitnames: [str] = ["Ada", "Grace"]const marks a binding immutable — reassigning it is an error:
const LIMIT = 100Functions
Section titled “Functions”func add(a: int, b: int) -> int { a + b}
func main() { println(add(3, 4)) // 7}main()The return type can be omitted — it is inferred from the body. Parameters can have defaults and types can be generic (see Language Reference):
func squareArea(a: int, b: int = 2) { return math.pow(a, b)}
println(squareArea(2, 2)) // 1024.0println(squareArea(2)) // uses default b = 2Structs
Section titled “Structs”struct User {id: int, name: str, email: str}
users: [User] = []users.append(User{id: 1, name: "Ada", email: "ada@example.com"})
for user in users { println("{user.name} <{user.email}>")}Methods live in impl blocks and take self first:
struct Rectangle {width: int, height: int}
impl Rectangle { func area(self) -> int { return self.width * self.height }}
rect := Rectangle{width: 3, height: 4}println(rect.area()) // 12Control flow
Section titled “Control flow”if temp > 30 { println("hot")} else { println("fine")}
for i in range(10) { println("Hello")}
n := 3while n > 0 { println(n) n = n - 1}if is also an expression — each branch yields a value:
func factorial(n: int) -> int { if n <= 1 { 1 } else { n * factorial(n - 1) }}func describe(x: int) -> str { match x { 0 => "zero", 1 => "one", _ => "many" }}match works on Option and Result variants too:
match int("42") { .some(v) => println("parsed: {v}"), .none => println("not a number"),}Closures and iterators
Section titled “Closures and iterators”squares := map(range(1, 6), |x: int| x * x) // [1, 4, 9, 16, 25]evens := filter(range(1, 11), |x: int| x % 2 == 0) // [2, 4, 6, 8, 10]pairs := zip(["alice", "bob"], [95, 87])Pipeline operator
Section titled “Pipeline operator”a |> f(b) desugars to f(a, b), and works across lines:
func double(x: int) -> int { x * 2 }func add_one(x: int) -> int { x + 1 }
result := 5 |> add_one |> doubleprintln(result) // 12Use _ to choose the argument position:
server := http.server() |> http.get(_, "/", |body: str| "Hello, World!")Elvis operator
Section titled “Elvis operator”?? unwraps an Option, or falls back — non-Option values pass through:
name := .some("Ada")println(name ?? "stranger") // Adaprintln(42 ?? 0) // 42String interpolation
Section titled “String interpolation”name := "ZZ"pi := 3.14159n := 255
println("Hello, {name}!")println("{pi:.2f}") // 3.14println("{n:x} {n:o} {n:b}") // ff 377 11111111println("{name} is {40 + 2}") // ZZ is 42List comprehensions
Section titled “List comprehensions”squared := [i ** 2 for i in range(0, 6)]odd := [i for i in range(21) if i % 2 != 0]Imports and comments
Section titled “Imports and comments”import std.strimport std.math
// line comment/* block comment */
number1: int = 2number2 := 2