Generics let one function handle values of different types. In Zig, a comptime type parameter makes the type available during compilation; the function works for a type when its body is valid for that type. This compile-time duck typing can also be supplemented with explicit type checks. section 4.1.3
Listing 4.2 Comptime type parameters for fun and profit
pub fn tokenizeScalar(
comptime T: type, #1
buffer: []const T, #2
delimiter: T, #3
) TokenIterator(T, .scalar) #4
The declaration shows how a comptime type parameter lets tokenizeScalar accept a buffer and delimiter of that type. section 4.1.3
A sum that checks its own limits
A generic sum function accepts a comptime type and a slice of values of that type. It works for integers, but passing bool fails because booleans cannot be added. section 4.1.3
Generic code combines comptime features such as type parameters with patterns such as interfaces. Zig’s compile-time type checking is presented as a concise alternative to C-style macros and C++ templates, with errors when an operation is invalid for a supplied type. section 4.4 section 4.1.3






















