Parameter anytype
Terima argumen apa pun dengan pengetikan bebek.
Parameter anytype adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 4 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.
Accept Anything with anytype
Sometimes you do not want to name the type at all. Declare a parameter as anytype and Zig infers its type from the argument. 🦆
fn show(x: anytype) void {}Type Is Inferred at the Call
With anytype there is no leading type parameter. Each caller's argument decides the concrete type for that specialization.
show(42);
show(true);This Is Duck Typing
Zig only checks that the body's operations work for the argument you pass. If it has the right shape, it fits. That is duck typing.
Recover the Type If You Need It
Inside the function you can still ask for the type using @TypeOf, then use it for locals or return values.
fn dup(x: anytype) @TypeOf(x) {
return x;
}Great for Print-Style Helpers
Functions that work on many shapes love anytype. Zig's own print accepts an anytype tuple of arguments to format.
std.debug.print("{any}\n", .{x});Constraints Come from the Body
There is no explicit interface. If you call x.len inside, then only arguments that actually have a len field will compile.
fn size(x: anytype) usize {
return x.len;
}Errors Are Per-Specialization
If a caller passes a type the body cannot handle, Zig reports the error at that call site, naming the exact type that failed.
Validate Inputs with comptime
For clearer errors you can inspect the type early with @typeInfo and reject the wrong shape using a compile-time check.
anytype vs comptime T: type
Use comptime T: type when you must name the type up front; reach for anytype when inference and flexibility matter more.
Still Zero Run-Time Cost
Like every Zig generic, an anytype function is specialized at compile time, so the inferred flexibility costs nothing when the program runs.
Mix anytype with Named Types
A function can blend both styles: one anytype parameter for flexible input alongside ordinary typed parameters for fixed data.
fn join(sep: u8, x: anytype) void {
_ = sep;
_ = x;
}Quick Check
You write fn show(x: anytype). How does Zig decide the concrete type of x?
Recap
The anytype keyword lets a parameter accept any argument, with the type inferred and the body checked per call. It is Zig's flexible duck typing. 🎯
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Parameter anytype” gratis?
Ya — teks lengkap “Parameter anytype” 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 “Parameter anytype”?
Terima argumen apa pun dengan pengetikan bebek. 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 4 dari 4.
Berapa lama pelajaran “Parameter anytype” 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.