Functions
Defining functions
Section titled “Defining functions”fn add(a: Int, b: Int): Int { a + b}- The last expression in the body is the return value (no
returnneeded). - All module-level functions are hoisted. You can call them before they’re defined.
- Trailing commas are allowed in parameter lists and argument lists.
Named arguments
Section titled “Named arguments”Every parameter can be passed by name at the call site:
fn greet(name: String, prefix: String): String { "${prefix}, ${name}!"}
debug greet(name: "Alice", prefix: "Hello") // Hello, Alice!debug greet(prefix: "Hi", name: "Bob") // Hi, Bob!. Any orderdebug greet("Carol", "Hey") // positional also worksDefault parameter values
Section titled “Default parameter values”fn greet(name: String, prefix: String = "Hello"): String { "${prefix}, ${name}!"}
debug greet(name: "Alice") // Hello, Alice!debug greet(name: "Bob", prefix: "Hi") // Hi, Bob!Return type inference
Section titled “Return type inference”Private functions don’t need an explicit return type. The compiler infers it from the body:
fn double(n: Int) { n * 2 } // inferred: Intfn noop() { } // inferred: Voidpub fn must always have an explicit return type. Module boundaries must be self-documenting.
Closures
Section titled “Closures”Functions are first-class values. Anonymous functions (closures) capture their surrounding scope:
let add5 = fn(x: Int): Int { x + 5 }debug add5(10) // 15
fn make_adder(base: Int): fn(Int): Int { fn(x: Int): Int { base + x } // captures 'base' from enclosing scope}
let add10 = make_adder(10)debug add10(3) // 13debug add10(7) // 17The pipe operator |>
Section titled “The pipe operator |>”|> passes the left-hand value as the first argument to the right-hand function:
fn double(n: Int): Int { n * 2 }fn inc(n: Int): Int { n + 1 }
debug 5 |> double // 10debug 5 |> double |> inc // 11Use _ to control where the piped value lands:
fn add(a: Int, b: Int): Int { a + b }
debug 3 |> add(_, 10) // add(3, 10) = 13debug 3 |> add(10, _) // add(10, 3) = 13Generic functions
Section titled “Generic functions”fn identity<T>(x: T): T { x }
debug identity(42) // 42debug identity("hello") // helloNext steps
Section titled “Next steps”- Functions reference: closures, destructured parameters, function type aliases
- Control Flow: using
returnfor early exit