@TypeOf and Type Reflection
Inspect types to stay generic.
@TypeOf and Type Reflection 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.
Asking About Types
Generic code often needs to know the type of a value without you spelling it out. Zig answers that with the built-in @TypeOf. 🔍
@TypeOf Returns a Type
Give @TypeOf one or more expressions and it returns their type, computed entirely at compile time.
const T = @TypeOf(value);It Does Not Run the Expression
The argument to @TypeOf is only inspected, never executed. Even a function call inside it is examined but not actually run.
Capture the Type of a Parameter
Inside a generic function you can name the type of any argument and reuse it, for example to declare a matching local variable.
fn echo(x: anytype) void {
const T = @TypeOf(x);
_ = T;
}Get a Readable Name
The built-in @typeName turns a type into a readable string, which is handy for debug printing or error messages.
const name = @typeName(i32);Deeper Reflection with @typeInfo
For full detail, @typeInfo returns a tagged union describing the type: whether it is an int, a struct, a pointer, and more.
const info = @typeInfo(i32);Branch on the Kind of Type
Because the result is a tagged union, you can switch on it to handle integers, floats, and structs differently in one generic function.
switch (@typeInfo(T)) {
.int => {},
else => {},
}Read an Integer's Bit Width
When a type is an integer, its info carries fields like bits and signedness, so you can adapt your logic to the exact width.
const bits = @typeInfo(u16).int.bits;Reflection Keeps Code Generic
By inspecting types instead of hard-coding them, one function can correctly serve many input types. That is the heart of Zig reflection. ✨
All at Compile Time
Every reflection built-in runs during the build, so this introspection adds zero run-time overhead to the finished program.
List a Struct's Fields
For a struct, @typeInfo exposes a fields array you can loop over at compile time to read each field's name and type.
const fields = @typeInfo(Point).@"struct".fields;Quick Check
Inside a generic function you have a value x and want its type. Which built-in gives it to you?
Recap
Use @TypeOf to learn a value's type and @typeInfo to inspect its structure. Switching on that info lets one function adapt to many types. 🎯
Frequently asked questions
Is the “@TypeOf and Type Reflection” lesson free?
Yes — the full text of “@TypeOf and Type Reflection” 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 “@TypeOf and Type Reflection”?
Inspect types to stay generic. 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 “@TypeOf and Type Reflection” 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
- Functions That Take a Type
- Generic Data Structures
- @TypeOf and Type Reflection
- anytype Parameters