XU XUv0.8.8
中文

Docs / Tutorial

04 · Functions and methods

No classes: struct plus methods. Value receiver is read-only; &mut receiver requires a mut 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 &mut Counter) inc() {
    c.n += 1
}

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

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

Build (from the language repo root; swap the binary for your platform):

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/.