Functions That Take a Type
Generics via comptime T parameters.
Generics, the Zig Way
Zig has no separate generics syntax. Instead, a function can accept a type as one of its parameters and use it like any other value. ✨
A Type Parameter Is comptime
Because a type must be known when the code is built, the type parameter is always marked comptime. The caller passes a real type.
fn first(comptime T: type, items: []const T) T {
return items[0];
}All lessons in this course
- Functions That Take a Type
- Generic Data Structures
- @TypeOf and Type Reflection
- anytype Parameters