Functions

Function syntax🔗

fn add(a: i32, b: i32) -> i32 {
    a + b       // last expression is the return value
}

fn greet(name: String) {
    println(f"Hello, {name}!")   // returns ()
}

Early return🔗

fn abs_val(n: i32) -> i32 {
    if n < 0 { return -n }
    n
}

Closures🔗

Closures capture their enclosing scope by value.

let multiplier = 3
let triple = |x: i32| x * multiplier
println(f"{triple(5)}")   // 15

Multi-statement closure:

let process = |x: i32| {
    let doubled = x * 2
    doubled + 1
}

Higher-order functions🔗

Arrays have built-in map, filter, and fold:

import { println } from "dogma:core"

fn main() {
    let numbers = [1, 2, 3, 4, 5]

    let doubled = numbers.map(|x| x * 2)
    // [2, 4, 6, 8, 10]

    let evens = numbers.filter(|x| x % 2 == 0)
    // [2, 4]

    let sum = numbers.fold(0, |acc, x| acc + x)
    // 15

    println(f"sum = {sum}")
}

String interpolation🔗

Use f"..." for interpolated strings. Any expression can appear inside {}.

let name = "Aria"
let hp = 100
let msg = f"Player {name} has {hp} HP"

Known limitation🔗

? inside a closure is unchecked — closure return-type tracking is conservative. If you need ? in a closure, extract it to a named function.