Types Are comptime Values
Treat a type like any other value.
Types Are comptime Values is a free Zig Academy lesson on CoddyKit — lesson 3 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.
A Type Is Just a Value
In Zig a type is not magic syntax: it is an ordinary value you can store, pass, and return. It simply lives only at compile-time. 🧠
The type of a Type Is type
Every type has the type type. So u8 is a value whose type is type, just like 5 is a value whose type is an integer.
const T: type = u8;Bind a Type to a const
You can name a type with a constant. Here Number becomes another name for i32 that you can use wherever a type is expected.
const Number = i32;
const x: Number = 7;Pass a Type to a Function
A function can take a type as a comptime parameter. The argument is a real value, evaluated while the program compiles.
fn sizeOf(comptime T: type) usize {
return @sizeOf(T);
}Return a Type from a Function
Because types are values, a function can return one. Its return type is just type, and the body produces a type as its result.
fn Pair() type {
return struct { a: u8, b: u8 };
}This Is How Generics Work
Passing and returning types is the foundation of Zig generics. There is no special generic syntax, just type values flowing around.
Types Live Only at Compile-Time
A value of type type exists only during compilation. You cannot store a type in a run-time variable or read one from input.
Inspect a Type with Builtins
Built-ins treat a type as data. @sizeOf takes a type value and returns how many bytes one instance of it occupies.
const bytes = @sizeOf(u32);Compare Types for Equality
Since types are values, you can compare them at compile-time. The expression T == u8 is true only when T is exactly u8.
Choose a Type at Compile-Time
You can branch on a type and pick another, all during compilation. This lets one function adapt its behavior to the type it gets.
Name a Type from a Function Result
Since a function can return a type, you can bind that result to a constant and then use it just like any built-in type name.
const Point = Pair();
const p: Point = .{ .a = 1, .b = 2 };Quick Check
In Zig, what is the type of the type u8 itself?
Recap
Types in Zig are comptime values of type type. You can name, pass, return, and compare them, which is exactly how generics are built. 🎯
Frequently asked questions
Is the “Types Are comptime Values” lesson free?
Yes — the full text of “Types Are comptime Values” 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 “Types Are comptime Values”?
Treat a type like any other value. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Types Are comptime Values” 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
- Compile-Time vs Run-Time
- comptime Parameters and Values
- Types Are comptime Values
- inline for and Unrolled Loops