Variadic packs
A trailing va: ... parameter collects a variable number of call-site arguments into a compile-time sequence. The compiler monomorphizes the function once per distinct argument type-list. There is no runtime structure, no va_list, and no any.
A pack and the C-variadic signature share only the spelling. A pack is a named parameter (va: ...) on a mach function, and it is the only variadic form a mach function takes. The C-variadic form is a bare trailing ..., declared only on an ext fun to describe a foreign callee. See External functions.
Declaring a pack
A pack parameter is a named parameter whose type is .... It must be last, and a parameter after it is refused with a variadic pack `...` must be the last parameter. Any number of fixed, typed parameters may precede it.
fun name(va: ...) RetType { ... }
fun name(fixed: T, va: ...) RetType { ... } # leading fixed params are allowed
Iterating with $each
$each a in va unrolls the body once per element, with a bound to the element and re-typed to that element's concrete type per instantiation. This is the only way to consume a pack's elements: a pack cannot be indexed, and its only member is .len.
fun sum(va: ...) i64 {
var t: i64 = 0;
$each a in va {
t = t + a;
}
ret t;
}
sum(1, 2, 3) # 6
sum() # 0, the body never runs on an empty pack
Because each element has its own concrete type at monomorphization, the body can handle a heterogeneous pack by casting each element. The body is checked once per element of each instance, and an element it does not type for is reported at the call that asked for the instance:
$each a in va {
t = t + a::i64; # cast each element's concrete type to i64
}
A $each body is a normal statement block. Runtime variables in the enclosing scope (a cursor, an accumulator) thread across every unrolled iteration: each one reads where the previous left off.
Counting with va.len
va.len is a u64 that folds to the instance's element count. It is not a comptime value, so a $if gate cannot read it and it cannot size an array type.
fun count(va: ...) i64 { ret va.len::i64; }
count(1, 2, 3) # 3
Forwarding a whole pack
Inside a pack instance, g(va...) forwards the whole pack to another pack-tailed function, which is monomorphized for the forwarded type-list.
fun outer(va: ...) i64 { ret sum(va...); }
va... is valid only as the sole trailing argument of a pack-tailed callee, though leading fixed arguments may precede it, as in bias(100, va...). Spreading into a callee with no pack parameter, spreading with other arguments after it, spreading only part of a pack, and passing the pack bare as g(va) are all rejected.
Monomorphization and ABI
A pack-tailed function is compiled once per distinct argument type-list at each call site. Different arities, or the same arity with different types, produce separate instances.
| Call | Instance |
|---|---|
sum(1, 2, 3) | (i64, i64, i64) |
sum(10, 20) | (i64, i64) |
sum(5::u8, 1::u32) | (u8, u32) |
A pack-tailed function has no single entry point, so it is not a stable-ABI symbol. A pack cannot appear on an ext fun or a body-less fun, and a pack-tailed function cannot be taken as a function pointer value. It is source-level only.
A pack tail composes with generic parameters, and the instance key is then the product of the type arguments and the element type-list.
A ^ secret may not be passed to a pack, including one nested inside an aggregate argument, since a pack commonly feeds formatting or logging. The call is refused with a secret value cannot be passed to a variadic `...` argument. A pointer to secret storage is admitted.
Example: vformat
The standard library's vformat is a pack-tailed function. Each $each iteration handles one format argument in order, with $type_of dispatch selecting the right writer per element type.
$each arg in va {
$if ($type_of(arg) == str) { write_str(w, arg); }
$or ($type_of(arg) == i64) { write_i64(w, arg); }
$or { $error("no writer for this argument type"); }
}
The runtime format cursor threads across all unrolled iterations; the argument sequence is consumed entirely at compile time.
See also
- Functions - declarations, generic and comptime parameters
- Intrinsics -
$type_of,$fields,$each - Control flow -
$ifand$orinside pack bodies