0Pricing
Zig Academy · Lektion

Typen sind comptime-Werte

Behandeln Sie einen Typ wie jeden anderen Wert.

Typen sind comptime-Werte 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.

A Type Is Just a Value

In Zig a type is not magic syntax: it is an ordinary value you can store, pass, and return. It simply lives only at compile-time. 🧠

The type of a Type Is type

Every type has the type type. So u8 is a value whose type is type, just like 5 is a value whose type is an integer.

const T: type = u8;

Bind a Type to a const

You can name a type with a constant. Here Number becomes another name for i32 that you can use wherever a type is expected.

const Number = i32;
const x: Number = 7;

Pass a Type to a Function

A function can take a type as a comptime parameter. The argument is a real value, evaluated while the program compiles.

fn sizeOf(comptime T: type) usize {
    return @sizeOf(T);
}

Return a Type from a Function

Because types are values, a function can return one. Its return type is just type, and the body produces a type as its result.

fn Pair() type {
    return struct { a: u8, b: u8 };
}

This Is How Generics Work

Passing and returning types is the foundation of Zig generics. There is no special generic syntax, just type values flowing around.

Types Live Only at Compile-Time

A value of type type exists only during compilation. You cannot store a type in a run-time variable or read one from input.

Inspect a Type with Builtins

Built-ins treat a type as data. @sizeOf takes a type value and returns how many bytes one instance of it occupies.

const bytes = @sizeOf(u32);

Compare Types for Equality

Since types are values, you can compare them at compile-time. The expression T == u8 is true only when T is exactly u8.

Choose a Type at Compile-Time

You can branch on a type and pick another, all during compilation. This lets one function adapt its behavior to the type it gets.

Name a Type from a Function Result

Since a function can return a type, you can bind that result to a constant and then use it just like any built-in type name.

const Point = Pair();
const p: Point = .{ .a = 1, .b = 2 };

Quick Check

In Zig, what is the type of the type u8 itself?

Recap

Types in Zig are comptime values of type type. You can name, pass, return, and compare them, which is exactly how generics are built. 🎯

Häufig gestellte Fragen

Ist die Lektion „Typen sind comptime-Werte“ kostenlos?

Ja — der vollständige Text von „Typen sind comptime-Werte“ 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 sind comptime-Werte“?

Behandeln Sie einen Typ wie jeden anderen Wert. 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 „Typen sind comptime-Werte“?

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

  1. Compilezeit und Laufzeit
  2. comptime-Parameter und -Werte
  3. Typen sind comptime-Werte
  4. inline for und entrollte Schleifen
← Zurück zu Zig Academy