Functions, Generics & Contracts
Declarations, typed and untyped parameters, generics by monomorphization, defer, and executable contracts.
Declaration
Parameters may be typed or untyped. A typed function is a candidate for native lowering; an untyped one takes the boxed path. A missing return type means the function returns no value. return exits immediately; a bare return; is allowed in a no-value function.
Named arguments and default parameter values are not supported. Call arguments are positional.
Generics
Generics are monomorphized: each distinct type tuple produces a separate C function. Recursive instantiation is detected and terminates with a diagnostic. Generic interfaces are supported too:
Native ABI
A function is eligible for an unboxed native variant when:
- every parameter is a primitive (
Int64,Float64,Float32,Bool), a typed array (Array<Float64>,Array<Int64>,Array<Float32>), or a struct with a registered field layout; - the return type is a primitive, typed array, or such a struct; and
- the body contains no top-level
deferortry.
Eligible functions are emitted twice: once with the boxed signature (int argc, VyneValue* argv) -> VyneValue, and once with a native signature such as:
Call sites prefer the native variant when every argument matches. Struct parameters expand to one C argument per field in declaration order; array parameters pass .data directly. On AVR this is what keeps numerical kernels off the 1 KB arena.
Defer
defer registers a statement or block to run when the enclosing scope exits, including early returns. Defers run in LIFO order.
Output order is body, second registered, first registered, then the returned value is observed by the caller. A function with a top-level defer is not eligible for the native ABI.
Contracts
requires runs at function entry; ensures runs before every return, normal or early. Inside ensures, result names the return value.
contracts mode | Effect |
|---|---|
"check" | Emit assertions; violation halts with VNE-111 |
"evidence" | Assert and append a CSV pass/fail row to vyne_contracts.log (or $VYNE_CONTRACT_LOG) |
"off" | Emit nothing (release path) |
Contracts are also available on loops as @invariant(...). See Control Flow.
Recursion
Recursion is supported on both backends. The native path handles direct recursion; mutual recursion works through the boxed call path when the signatures cannot be specialized.
Calling convention summary
| Call form | Example |
|---|---|
| Plain function | add(2, 3) |
| Generic | identity<Float64>(1.0) |
| Module function | vmath.sqrt(9.0) |
| Group function | Geometry.area_of_circle(2.0) |
| Interface method | circle.area() |
| Pipeline | 5 |> double |