Functions and Lambdas
Declarations, generics, purity, first-class functions, closures, and method syntax.
Contents
A function may be a free function, a trait method, or a lambda expression. All three use the same call syntax.
Declaring a Function#
A declaration begins with fn, followed by the name, type parameters, parameter list, and return type. Parameters are written as name followed by type. A function without a return type returns void.
fn greet(name str) {
print($"hello, {name}")
}
fn add(a i32, b i32) i32 => a + b
The body is either an expression after => or a block. A block may use return, or end with an expression assignable to the return type.
fn clamp(n i32, low i32, high i32) i32 {
if n < low {
return low
}
if n > high {
return high
}
n
}
Modifiers#
pub makes a declaration visible to other modules. pure permits compile-time evaluation and use inside type invariants. The modifiers may appear in either order before fn.
pub pure fn double(n i32) i32 => n * 2
A type invariant may call only pure functions. Only pure functions are folded at compile time. A pure function may not call an impure function or an impure builtin such as print. Purity of indirect calls through function values is not tracked.
extern functions have no Haze body and no contracts. They cannot be pure and cannot be used as values. See Extern and C FFI.
Generic Functions#
Type parameters are declared in square brackets after the name and may carry trait bounds. The compiler infers arguments at each call site; there is no turbofish syntax.
fn identity[T](value T) T => value
fn maximum[T Ord](a T, b T) T {
if a.lt(b) {
return b
}
return a
}
Bounds are separated from the parameter name by a space, as in T Ord. A parameterized bound uses brackets, as in T Convert[i64]. See Generics and Traits.
Contracts#
Contract clauses appear after the return type and before the body.
fn divide(numerator i32, denominator i32) i32
: pre denominator != 0
: post r => r * denominator <= numerator
=> numerator / denominator
A contract is part of the signature: callers may rely on a postcondition, and the compiler may use a precondition to reject an impossible call. See Design by Contract.
Method-Call Syntax#
receiver.name(arguments) is rewritten to name(receiver, arguments). This works for free functions and trait methods.
fn scale(value i32, factor i32) i32 => value * factor
fn main() {
print((3).scale(4))
print([1, 2, 3].len)
}
A trait method call resolves through the receiver's type and its implementations. If no trait method matches, the compiler falls back to a free function with the same name.
const names = people
.filter(is_adult)
.map(fn(person) => person.name)
Lambdas#
A lambda begins with fn followed by its parameter list. Parameter types may be omitted when an expected function type supplies them. The body is an expression after => or a block.
const double = fn(x i32) i32 => x * 2
const add_one = fn(x) => x + 1
A lambda used as an argument is checked against the corresponding parameter type.
fn apply[T, U](value T, transform fn(T) U) U => transform(value)
fn main() {
print(apply(3, fn(x) => x * 2))
}
The function type syntax is fn(T, U) R. A function value may be stored in a const, passed to another function, or returned. Generic and extern functions cannot be used as values.
Closures#
A lambda captures the local bindings it mentions by value. Rebinding the outer variable later does not change the captured copy. For a managed value, the copy is a reference, so mutations to fields or elements remain visible.
fn make_adder(base i32) fn(i32) i32 => fn(x) => base + x
fn main() {
const add_ten = make_adder(10)
print(add_ten(5))
}
A closure is a managed value. Its captured environment is traced by the collector while the closure is reachable.
Recursion#
There is no special syntax and no tail-call guarantee.
pure fn factorial(n i32) i32
: pre n >= 0
: pre n <= 12
: post r => r >= 1
{
if n <= 1 {
return 1
}
return n * factorial(n - 1)
}
Overloading#
Multiple functions with the same name in a module are rejected. A function and a constant may not share a name in the same module. Use distinct names or a trait with multiple implementations.
fn render_i32(value i32) str => $"i32({value})"
fn render_str(value str) str => $"str({value})"
Entry Point#
The entry module must define main. It takes no parameters. It may return void, or i32, which becomes the process exit status.
fn main() i32 {
print("done")
return 0
}
Process arguments are not passed to main. The standard environment module exposes them as a list of strings. See Modules.