0Pricing
Zig Academy · Lesson

Functions That Take a Type

Generics via comptime T parameters.

Functions That Take a Type is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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];
}

type Is a Real Type

In Zig the word type is itself a type, so a parameter can be declared as type to mean any type at all.

comptime T: type

Use T in the Signature

Once you name the type T, you can use it for later parameters and the return type, tying them all together.

fn max(comptime T: type, a: T, b: T) T {
    return if (a > b) a else b;
}

Calling a Generic Function

To call it, pass the concrete type first, then the regular arguments. Here we ask for the larger of two i32 values.

const m = max(i32, 3, 9);

One Function, Many Types

The same function works for any type that supports the operations you use. Swap i32 for f64 and the code still fits.

const f = max(f64, 1.5, 2.5);

Zig Specializes Each Call

For every distinct type you pass, Zig generates a dedicated copy of the function. This is monomorphization, done at compile time.

The Body Stays Generic

You write the type explicitly at the call, but the body stays generic. Zig checks that the operations you use are valid for whatever T arrives.

type Means No Hidden Boxing

Passing a real type is different from runtime polymorphism: there is no vtable and no boxing, just a concrete copy chosen at build time.

Errors Surface at Compile Time

If you call max with a type that has no greater-than operator, Zig reports the problem when it builds that specialization, not at run time.

Type Parameters Come First

By convention the comptime type parameter is listed before the value parameters, so the type is known before the data is described.

fn clone(comptime T: type, value: T) T {
    return value;
}

Quick Check

You want a function that works for many types in Zig. How do you declare its type parameter?

Recap

Pass a comptime T: type parameter to make a function generic. Zig builds a specialized version for every type you actually use. 🎯

Frequently asked questions

Is the “Functions That Take a Type” lesson free?

Yes — the full text of “Functions That Take a Type” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Functions That Take a Type”?

Generics via comptime T parameters. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Functions That Take a Type” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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