Language

Readable blocks

Readable blocks keep related steps together while preserving their written order. Each block uses braces and newlines. Names declared inside do, collect, with, and attempt stay local to that expression.

Group related bindings

Use let { ... } or mut { ... } when adjacent bindings belong to one task. Each entry is evaluated from top to bottom, so later entries can use earlier ones.

let {
    first = 20
    second: Int = first + 22
}
print(second)  # 42

Use mut for bindings that will change after the group is initialized:

mut {
    count = 0
    finished = False
}
count = count + 1
finished = count == 1
print(finished)  # true

Produce one value from several steps

do evaluates statements in order and takes its value from the final expression. A return inside it still returns from the enclosing function.

let answer = do {
    let base = 20
    let extra = 22
    base + extra
}
print(answer)  # 42

Bind named data fields

Named destructuring binds by field name, even if the fields appear in a different order from their declaration. Tuple destructuring remains available for positional results.

data Person {
    name: Str
    age: Int
}
let Person { age, name } = Person("Ada", 42)
print(name)
print(age)

Select and project list items

collect evaluates its source once, then visits items in order. Every where clause must be true before the final yield is evaluated for that item.

let doubled_evens = collect number in [1, 2, 3, 4] {
    where number % 2 == 0
    yield number * 2
}
print(doubled_evens)  # [4, 8]

Keep eager list operations in order

transform uses the same eager filter, map, and sorted operations as the builtins. Each step receives the preceding step's whole list.

pure def positive(x: Int) -> Bool { return x > 0 }
pure def double(x: Int) -> Int { return x * 2 }
let values = transform [-2, 3, 1] {
    filter positive
    map double
    sort
}
print(values)  # [2, 6]

Work with one explicit receiver

with source as name evaluates the source once, binds name locally, and returns the final expression. Method calls still name their receiver.

class Counter {
    mut value: Int = 0
    def add(self, amount: Int) -> None { self.value = self.value + amount }
    def read(self) -> Int { return self.value }
}
let total = with Counter() as counter {
    counter.add(2)
    counter.read()
}
print(total)  # 2

Stop on the first Result error

attempt ends with one yield. A successful path wraps that value in Ok; a failed ? exits the block as Err and leaves the enclosing function running. Its fallible steps must have compatible error types.

pure def load(id: Int) -> Result[Int, Str] {
    if id < 0 { return Err("missing") }
    return Ok(id)
}
let answer = attempt {
    let first = load(20)?
    let second = load(22)?
    yield first + second
}
print(answer)  # Ok(42)

Run vary check --great-code to see advisory suggestions for clear grouped declarations, value chains, list selection, and ordered list transforms. The normal vary check profile accepts other valid forms.

See syntax overview, collections and strings, and the example programs for surrounding context.

← Error handling
Modules and imports →