Functions, Closures, and Generics
Tyrus supports arrow functions and native function values.
Functions
const add = (left: i32, right: i32): i32 => {
return left + right;
};
const double = (n: i32): i32 => n * 2;
const greet = (name: string): void => {
print("hello");
print(name); // "name value"
};Function signatures include parameter and return types. Expression bodies return implicitly. A non-void function must return a value on every reachable path.
Function Values
const double: (i32) => i32 = (value: i32): i32 => value * 2;
const result: i32 = double(21);Function values can be stored and called. They may capture lexical values, subject to ownership and lifetime rules.
A named function is itself a first-class value: it can be passed as an argument, aliased to another binding, and used as an array callback. A top-level const name = (…) => … declares a named function, so the same applies to it.
const double = (value: i32): i32 => value * 2;
const apply = (f: (i32) => i32, v: i32): i32 => f(v);
print(apply(double, 21)); // passed as an argument
const numbers: i32[] = [1, 2, 3];
const doubled: i32[] = numbers.map(double); // used as a callbackA function can also return a function. The returned value keeps its function type, so a binding that receives it stays callable without an annotation.
const makeAdder = (base: i32): (value: i32) => i32 => {
return (value: i32): i32 => base + value;
};
const add5 = makeAdder(5); // inferred as `(i32) => i32`
print(add5(3)); // 8The returned function can also be called directly, without a binding in between. The callee is then an expression rather than a name, so it carries no signature to look up: its type is the whole contract, and calling something that is not a function value is E0618 with the range on the callee.
print(makeAdder(5)(3)); // 8
print(makeUserFactory()().id);Capture across two levels is not supported yet
A closure that captures a binding from two levels out, as in (a) => (b) => (c) => a + b + c, is rejected with E0435, anchored on the mention that reaches too far. A closure can only capture from the function directly around it.
Mutable Parameters
const increment = (mut n: i32): i32 => {
n += 1;
return n;
};mut allows reassignment inside the function body without making the caller binding mutable.
Rest Parameters
const sum = (...values: i32[]): i32 => {
let total: i32 = 0;
for (const value of values) {
total += value;
}
return total;
};
const numbers: i32[] = [1, 2, 3];
print(sum(1, 2, 3)); // individual arguments
print(sum(...numbers)); // spread of an array
print(sum()); // no argumentsA rest parameter (...name: T[]) must be the last parameter and collects the trailing arguments into an array. It accepts individual arguments, a ...spread of an existing array, no arguments at all, and any combination alongside leading fixed parameters. Individual arguments are checked against the element type; a spread argument is checked against the whole array type.
Closures
const factor: i32 = 3;
const offset: i32 = 2;
const transform: (i32) => i32 = (value: i32): i32 => value * factor + offset;
print(transform(10)); // 32Function values use a native closure representation with a function pointer and an environment pointer. Synchronous functional-array callbacks can capture lexical values without transferring ownership of the captured resource.
What a Capture Sees
A closure that never leaves the function that declared it captures the bindings themselves. Reads see the current value, writes reach the binding outside, and state accumulates across calls:
function main(): void {
let counter: i32 = 0;
const bump = (): i32 => {
counter = counter + 1;
return counter;
};
print(bump()); // 1
print(bump()); // 2
print(counter); // 2
}A closure only stays inside its function when every mention of it is a call. Returning it, passing it as an argument, aliasing it to another name, or storing it anywhere hands its value to something that may outlive the function, and then the bindings it captured are gone before it runs.
Such a closure captures a copy instead, taken when the closure is built. Reading a copy is well defined. Writing one is not useful: the write lands in the copy, and nothing outside can ever observe it. That is E0436, anchored on the write itself:
error
function make(): () => i32 {
let counter: i32 = 0;
return (): i32 => {
counter = counter + 1;
return counter;
};
}error[E0436]: cannot assign to `counter` from inside a closure that outlives its scope
at /src/main.tyr:4:9
counter = counter + 1;
^^^^^^^^^^^^^^^^^^^^^^
help: this closure escapes the function that declared `counter`, so it captures a copy; the write lands in that copy and nothing can observe it
help: return the value from the closure instead of writing `counter`, or hold the state in a `Mutex` both sides can reachTIP
The fix is to return the value instead of writing it, or to hold the shared state in a Mutex that both sides can reach. The second is what a task started with spawn needs, and the same rule is what keeps a task from sharing mutable state with the code that started it.
Generics
const identity = <T>(value: T): T => value;
const first = <T extends string>(value: T): T => value;
const number: i32 = identity<i32>(42);
const message: string = identity("tyrus");
const constrained: string = first("native");Generic functions use native monomorphization: the compiler emits one LLVM specialization per concrete type combination. Generic substitution works through arrays, references, optionals and function types.
