0Pricing
Zig Academy · Pelajaran

Mencerminkan Field dengan @typeInfo

Periksa struktur tipe saat kompilasi.

Mencerminkan Field dengan @typeInfo adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Zig Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Zig Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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. 🎯

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Mencerminkan Field dengan @typeInfo” gratis?

Ya — teks lengkap “Mencerminkan Field dengan @typeInfo” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Zig Academy, upgrade ke CoddyKit PRO. Kursus Zig Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Mencerminkan Field dengan @typeInfo”?

Periksa struktur tipe saat kompilasi. Kamu berlatih Zig Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Zig Academy?

Tidak diperlukan pengalaman sebelumnya. Zig Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.

Berapa lama pelajaran “Mencerminkan Field dengan @typeInfo” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Zig Academy ini?

Ya. Setiap pelajaran Zig Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Mencerminkan Field dengan @typeInfo
  2. Membangun Tipe dengan @Type
  3. Validasi Comptime dan @compileError
  4. Membuat Kode dengan comptime
← Kembali ke Zig Academy