Typen mit @Type erstellen
Erstellen Sie neue Typen programmgesteuert.
Typen mit @Type erstellen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.
The Reverse of @typeInfo
If @typeInfo turns a type into data, @Type turns data back into a real type. Together they let you reshape types at compile time. 🛠️
Feed It a Type.Info Value
@Type takes a std.builtin.Type value and produces the concrete type it describes. You hand it a description, it gives you a type.
const T = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = 8 } });Build an Integer Type
The example above constructs u8 from scratch. Change bits to 16 and you get u16, all decided while the program compiles.
const U16 = @Type(.{ .Int = .{ .signedness = .unsigned, .bits = 16 } });Round-Trip a Type
Passing a type through @typeInfo then @Type returns the very same type. This proves the two builtins are exact inverses.
const Same = @Type(@typeInfo(u32));Describe a Struct as Data
To build a struct you fill in a StructField array. Each field needs a name, a type, alignment, and a default pointer.
const f = std.builtin.Type.StructField;Assemble the Struct Payload
Wrap your fields in a .Struct payload with a layout and an is_tuple flag, then hand the whole thing to @Type.
const info = .{ .Struct = .{
.layout = .auto,
.fields = my_fields,
.decls = &.{},
.is_tuple = false,
} };Generate a Fresh Struct Type
Call @Type on that payload and you get a brand-new struct type, built by your own logic rather than written by hand.
const Generated = @Type(info);Defaults Need a Comptime Pointer
A field's default_value is an optional const pointer to the value, or null when there is no default. The pointer must be comptime-known.
const dv: ?*const anyopaque = &@as(u8, 0);Build Enums and More
The same trick works beyond structs: the .Enum payload lets @Type synthesize enum types from a computed list of members.
Why Construct Types at All
Generated types power serializers and ORMs: derive a packed config or row type automatically instead of editing it by hand. ✨
Pair It with Reflection
The strongest pattern reads a type with @typeInfo, transforms the data, then rebuilds a new type with @Type, like adding a field.
Quick Check
You have a std.builtin.Type value describing a struct. What turns it into a usable type?
Recap
@Type builds real types from Type.Info data, the inverse of @typeInfo. Pair them to read, transform, and regenerate types. 🎯
Häufig gestellte Fragen
Ist die Lektion „Typen mit @Type erstellen“ kostenlos?
Ja — der vollständige Text von „Typen mit @Type erstellen“ 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 „Typen mit @Type erstellen“?
Erstellen Sie neue Typen programmgesteuert. 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 2 von 4.
Wie lange dauert die Lektion „Typen mit @Type erstellen“?
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
- Felder mit @typeInfo reflektieren
- Typen mit @Type erstellen
- Comptime-Validierung und @compileError
- Code mit comptime generieren