Generic Data Structures
A type-returning function pattern.
Generic Containers in Zig
To make a data structure work for any element type, you write a function that takes a type and returns a brand-new struct type. 📦
A Function That Returns a Type
The trick is that a function can have a return type of type. It computes and hands back a fresh type at compile time.
fn List(comptime T: type) type {
return struct {};
}All lessons in this course
- Functions That Take a Type
- Generic Data Structures
- @TypeOf and Type Reflection
- anytype Parameters