Funktionen, die einen Typ übernehmen
Generics mit comptime-T-Parametern.
Funktionen, die einen Typ übernehmen ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.
Generics, the Zig Way
Zig has no separate generics syntax. Instead, a function can accept a type as one of its parameters and use it like any other value. ✨
A Type Parameter Is comptime
Because a type must be known when the code is built, the type parameter is always marked comptime. The caller passes a real type.
fn first(comptime T: type, items: []const T) T {
return items[0];
}type Is a Real Type
In Zig the word type is itself a type, so a parameter can be declared as type to mean any type at all.
comptime T: typeUse T in the Signature
Once you name the type T, you can use it for later parameters and the return type, tying them all together.
fn max(comptime T: type, a: T, b: T) T {
return if (a > b) a else b;
}Calling a Generic Function
To call it, pass the concrete type first, then the regular arguments. Here we ask for the larger of two i32 values.
const m = max(i32, 3, 9);One Function, Many Types
The same function works for any type that supports the operations you use. Swap i32 for f64 and the code still fits.
const f = max(f64, 1.5, 2.5);Zig Specializes Each Call
For every distinct type you pass, Zig generates a dedicated copy of the function. This is monomorphization, done at compile time.
The Body Stays Generic
You write the type explicitly at the call, but the body stays generic. Zig checks that the operations you use are valid for whatever T arrives.
type Means No Hidden Boxing
Passing a real type is different from runtime polymorphism: there is no vtable and no boxing, just a concrete copy chosen at build time.
Errors Surface at Compile Time
If you call max with a type that has no greater-than operator, Zig reports the problem when it builds that specialization, not at run time.
Type Parameters Come First
By convention the comptime type parameter is listed before the value parameters, so the type is known before the data is described.
fn clone(comptime T: type, value: T) T {
return value;
}Quick Check
You want a function that works for many types in Zig. How do you declare its type parameter?
Recap
Pass a comptime T: type parameter to make a function generic. Zig builds a specialized version for every type you actually use. 🎯
Häufig gestellte Fragen
Ist die Lektion „Funktionen, die einen Typ übernehmen“ kostenlos?
Ja — der vollständige Text von „Funktionen, die einen Typ übernehmen“ 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 „Funktionen, die einen Typ übernehmen“?
Generics mit comptime-T-Parametern. 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 1 von 4.
Wie lange dauert die Lektion „Funktionen, die einen Typ übernehmen“?
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