Functions
Named functions use def, type-first parameters, and an optional return type.
def greet(string name) string (
return "Hello, " ++ name
)A function with no declared return type may return any value. Declare the type when callers depend on it.
Lambdas
Use -> for an anonymous function:
auto double = (int value) int -> value * 2
int[] doubled = [1, 2, 3].map(double)A block lambda uses def:
auto validate = def(object record) result -> (
if record.id == null return result.err("missing id")
return result.ok(record)
)Lambdas capture variables from their surrounding scope.
Optional and rest parameters
A trailing T? parameter may be omitted and receives null:
Use ...name to collect extra arguments:
Spread an array at a call site:
Function types
Name a reusable signature with type and def:
The compiler checks arguments and return values when a function has a signature type.
Generics
Generic functions put type parameters after the name. This form is useful when a runtime assertion should preserve the requested type:
Generic result types preserve both success and error types:
Calling and binding
Functions are values. .call(...) invokes a function, and .bind(...) returns a function with leading arguments fixed.
Side-effect calls
OSL warns when code discards a meaningful return value. Prefix a call with void when discarding the result is deliberate:
This is common for mutating methods that also return the changed value.
Last updated