@TypeOf und Typreflexion
Untersuchen Sie Typen, um generisch zu bleiben.
@TypeOf und Typreflexion ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „@TypeOf und Typreflexion“ kostenlos?
Ja — der vollständige Text von „@TypeOf und Typreflexion“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „@TypeOf und Typreflexion“?
Untersuchen Sie Typen, um generisch zu bleiben. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „@TypeOf und Typreflexion“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Funktionen, die einen Typ übernehmen
- Generische Datenstrukturen
- @TypeOf und Typreflexion
- anytype-Parameter