Tür Alan İşlevler
comptime T parametreleriyle genel programlama.
Tür Alan İşlevler, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Generics, the Zig Way
Zig has no separate generics syntax. Instead, a function can accept a type as one of its parameters and use it like any other value. ✨
A Type Parameter Is comptime
Because a type must be known when the code is built, the type parameter is always marked comptime. The caller passes a real type.
fn first(comptime T: type, items: []const T) T {
return items[0];
}type Is a Real Type
In Zig the word type is itself a type, so a parameter can be declared as type to mean any type at all.
comptime T: typeUse T in the Signature
Once you name the type T, you can use it for later parameters and the return type, tying them all together.
fn max(comptime T: type, a: T, b: T) T {
return if (a > b) a else b;
}Calling a Generic Function
To call it, pass the concrete type first, then the regular arguments. Here we ask for the larger of two i32 values.
const m = max(i32, 3, 9);One Function, Many Types
The same function works for any type that supports the operations you use. Swap i32 for f64 and the code still fits.
const f = max(f64, 1.5, 2.5);Zig Specializes Each Call
For every distinct type you pass, Zig generates a dedicated copy of the function. This is monomorphization, done at compile time.
The Body Stays Generic
You write the type explicitly at the call, but the body stays generic. Zig checks that the operations you use are valid for whatever T arrives.
type Means No Hidden Boxing
Passing a real type is different from runtime polymorphism: there is no vtable and no boxing, just a concrete copy chosen at build time.
Errors Surface at Compile Time
If you call max with a type that has no greater-than operator, Zig reports the problem when it builds that specialization, not at run time.
Type Parameters Come First
By convention the comptime type parameter is listed before the value parameters, so the type is known before the data is described.
fn clone(comptime T: type, value: T) T {
return value;
}Quick Check
You want a function that works for many types in Zig. How do you declare its type parameter?
Recap
Pass a comptime T: type parameter to make a function generic. Zig builds a specialized version for every type you actually use. 🎯
Sıkça Sorulan Sorular
“Tür Alan İşlevler” dersi ücretsiz mi?
Evet — “Tür Alan İşlevler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.
“Tür Alan İşlevler” dersinde ne öğreneceğim?
comptime T parametreleriyle genel programlama. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Zig Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Tür Alan İşlevler” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Tür Alan İşlevler
- Genel Veri Yapıları
- @TypeOf ve Tür Yansıtma
- anytype Parametreleri