XU XUv0.16.0

04 · Functions and methods

No classes: struct plus methods. Value receiver is read-only; $& receiver requires a $ binding.

Source

@main

fn add(a int, b int) int {
    << a + b
}

struct Counter {
    n int
}

fn (c Counter) get() int {
    << c.n
}

fn (c $&Counter) inc() {
    c.n += 1
}

fn main() int {
    println(add(2, 3))

    $c := Counter{n: 10}
    println(c.get())
    c.inc()
    println(c.get())
    << 0
}

Build:

xu run examples/04_functions_methods/main.xu
# or: xu build examples/04_functions_methods/main.xu
# default artifact is ./main on mac/linux, ./main.exe on Windows

Run from the separate examples repository root; source files live under examples/.