Reflecting Fields with @typeInfo
Inspect a type's structure at compile time.
Reflecting Fields with @typeInfo 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.
Look Inside Any Type
Zig lets your code examine its own types while it compiles. The built-in @typeInfo hands you the full structure of a type as data. 🔎
It Returns a Type.Info Value
@typeInfo takes a type and returns a std.builtin.Type value. That value is a tagged union describing exactly what kind of type you passed.
const info = @typeInfo(u32);The Tag Says What Kind It Is
The union tag tells you the category: a struct, an int, a pointer, and so on. You switch on it to react to each kind of type.
switch (@typeInfo(T)) {
.Struct => {},
else => {},
}Reach the Struct Payload
When the type is a struct, the .Struct variant carries a payload describing its fields, declarations, and layout.
const s = @typeInfo(Point).Struct;Fields Live in an Array
The struct payload has a fields array. Each entry is a StructField holding one field's name, type, and other details.
const fields = @typeInfo(Point).Struct.fields;Read a Field's Name and Type
Each StructField exposes a name string and a type. Both are compile-time values you can print or branch on.
const f = @typeInfo(Point).Struct.fields[0];
const n = f.name;Walk Fields with inline for
Because the field list is comptime, you iterate it with inline for. The loop unrolls, giving each field its own concrete code.
inline for (@typeInfo(T).Struct.fields) |field| {
_ = field.name;
}Inspect Integer Types Too
For an int, the .Int variant reveals its signedness and bit width, so you can adapt logic to the exact number type.
const i = @typeInfo(i16).Int;
const bits = i.bits;Enums Expose Their Members
The .Enum variant lists every enum field with its name and integer value, letting you generate code per option.
const e = @typeInfo(Color).Enum;
const fields = e.fields;Pointers Reveal Their Target
For pointers the .Pointer variant tells you the child type, size kind, and whether it is const, so you can unwrap layers.
const p = @typeInfo(*u8).Pointer;
const child = p.child;This Powers Real Libraries
Reflection drives Zig's own JSON and formatting code: they read fields with @typeInfo and handle any struct you give them. ✨
Quick Check
You want the list of a struct's fields at compile time. Which expression gives it to you?
Recap
@typeInfo turns a type into data: switch on its tag, then read fields, bits, or members and loop them with inline for. 🎯
Frequently asked questions
Is the “Reflecting Fields with @typeInfo” lesson free?
Yes — the full text of “Reflecting Fields with @typeInfo” 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 “Reflecting Fields with @typeInfo”?
Inspect a type's structure at compile time. 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 “Reflecting Fields with @typeInfo” 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
- Reflecting Fields with @typeInfo
- Building Types with @Type
- Comptime Validation and @compileError
- Generating Code with comptime