Struktur Data Generik
Pola fungsi yang mengembalikan tipe.
Struktur Data Generik adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 2 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.
Generic Containers in Zig
To make a data structure work for any element type, you write a function that takes a type and returns a brand-new struct type. 📦
A Function That Returns a Type
The trick is that a function can have a return type of type. It computes and hands back a fresh type at compile time.
fn List(comptime T: type) type {
return struct {};
}Build the Struct Inside
Inside the function, define a struct whose fields use the type parameter T. Each call produces a struct tailored to that element type.
fn Box(comptime T: type) type {
return struct { value: T };
}Name the Resulting Type
Call the type-returning function and store the result in a const. Now you have a concrete type ready to use.
const IntBox = Box(i32);Create an Instance
Use that named type just like any struct. Here we make a Box that holds the integer forty-two.
const b = IntBox{ .value = 42 };Add Methods to the Inner Struct
The returned struct can contain methods too. They see the type parameter, so they stay fully generic.
fn Box(comptime T: type) type {
return struct {
value: T,
fn get(self: @This()) T {
return self.value;
}
};
}Reach the Type with @This
Inside an anonymous returned struct you cannot name it directly, so methods use @This() to refer to the enclosing struct type.
self: @This()Many Types from One Definition
Call the function with different types and you get distinct structs. Box(i32) and Box(f64) are separate, fully checked types.
const FloatBox = Box(f64);This Powers the Standard Library
Zig's own ArrayList and HashMap are built this exact way: functions that take a type and return a configured container type.
const Ints = std.ArrayList(i32);Resolved Entirely at Compile Time
All of this type construction happens during the build, so a generic container has the same cost as a hand-written one.
Carry an Allocator When Growing
Containers that grow store an allocator field, passed in at init, so the generic type stays explicit about where its memory comes from.
return struct {
items: []T,
alloc: std.mem.Allocator,
};Quick Check
You want a generic stack that works for any element type in Zig. What pattern do you use?
Recap
Write a function that takes a type and returns a struct type. Each call yields a concrete, specialized container. This is how std builds its collections. 🎯
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Struktur Data Generik” gratis?
Ya — teks lengkap “Struktur Data Generik” 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 “Struktur Data Generik”?
Pola fungsi yang mengembalikan tipe. 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 2 dari 4.
Berapa lama pelajaran “Struktur Data Generik” 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
- Fungsi yang Menerima Tipe
- Struktur Data Generik
- @TypeOf dan Refleksi Tipe
- Parameter anytype