0Pricing
Zig Academy · Lesson

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

  1. Functions That Take a Type
  2. Generic Data Structures
  3. @TypeOf and Type Reflection
  4. anytype Parameters
← Back to Zig Academy