0Pricing
Zig Academy · Pelajaran

@TypeOf dan Refleksi Tipe

Periksa tipe agar tetap generik.

@TypeOf dan Refleksi Tipe adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 3 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.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “@TypeOf dan Refleksi Tipe” gratis?

Ya — teks lengkap “@TypeOf dan Refleksi Tipe” 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 “@TypeOf dan Refleksi Tipe”?

Periksa tipe agar tetap generik. 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 3 dari 4.

Berapa lama pelajaran “@TypeOf dan Refleksi Tipe” 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. Fungsi yang Menerima Tipe
  2. Struktur Data Generik
  3. @TypeOf dan Refleksi Tipe
  4. Parameter anytype
← Kembali ke Zig Academy