Pass function values

A named, fixed-signature function is a value. Its type spells parameter types and a return type: fn(int) -> int. Store it, return it, or pass it to a higher-order function.

Indirect calls use exact positional arguments. Defaults and names are properties of a known declaration, and the function value's type does not carry them.

fn double(value: int) -> int { value * 2 }

fn apply(f: fn(int) -> int, value: int) -> int {
  f(value)
}

fn main() {
  let operation = double
  println(apply(operation, 4))
}
Output & diagnostics
Expected:
8
Ctrl/⌘+Enter runs the current lesson.