0Pricing
Zig Academy · Ders

Genel Veri Yapıları

Tür döndüren bir işlev kalıbı.

Genel Veri Yapıları, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 2. 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.

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

Sıkça Sorulan Sorular

“Genel Veri Yapıları” dersi ücretsiz mi?

Evet — “Genel Veri Yapıları” 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.

“Genel Veri Yapıları” dersinde ne öğreneceğim?

Tür döndüren bir işlev kalıbı. 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 2. dersidir.

“Genel Veri Yapıları” 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

  1. Tür Alan İşlevler
  2. Genel Veri Yapıları
  3. @TypeOf ve Tür Yansıtma
  4. anytype Parametreleri
← Zig Academy Sayfasına Dön