0Pricing
Zig Academy · Ders

Birden Çok Türden Birini Tutan Birleşimler

Alternatif biçimleri tek bir yuvada saklayın.

Birden Çok Türden Birini Tutan Birleşimler, 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.

One Slot, Many Shapes

Sometimes a value could be an integer or a float, but only one at a time. A union stores exactly one of several possible types in a single slot.

Declaring a Union

You write union and list named fields, each with its own type. The value holds just one of those fields at any moment.

const Number = union { int: i64, float: f64 };

It Holds One Field at a Time

Unlike a struct, a union does not keep every field at once. It stores a single active field, and the others are not present.

Setting the Active Field

You initialize a union by choosing one field with the dot syntax. That field becomes the active one for the value.

var n = Number{ .int = 42 };

Reading the Active Field

You read a union through the field you set. Access n.int and you get the integer you stored, just like reaching into a struct.

const x = n.int;

Memory Is Shared

A union reserves room for its largest field and reuses that space. This makes it a compact way to hold one of several variants.

Switching the Active Field

Assigning a new field makes that field the active one and the old value goes away. The union now represents a different shape.

n = Number{ .float = 3.14 };

Reading the Wrong Field

If you read a field that is not active, that is a bug. In safe builds Zig detects the wrong access and triggers a panic.

const f = n.int; // wrong field active

A Bare Union Has No Tag

A plain union does not remember which field is active. You must track that yourself, which is exactly the problem a tagged union solves.

Unions vs Structs

A struct is a logical AND of its fields, all present together. A union is a logical OR: just one field lives at a time.

When to Reach for a Union

Use a union when a value naturally takes one of a few forms, like a token that is a number or a symbol, never both at once.

Quick Check

How many of a union's fields hold a value at the same time?

Recap

You met unions: one slot that holds a single field from several typed options. They share memory, so set and read only the active field. 🔀

Sıkça Sorulan Sorular

“Birden Çok Türden Birini Tutan Birleşimler” dersi ücretsiz mi?

Evet — “Birden Çok Türden Birini Tutan Birleşimler” 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.

“Birden Çok Türden Birini Tutan Birleşimler” dersinde ne öğreneceğim?

Alternatif biçimleri tek bir yuvada saklayın. 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.

“Birden Çok Türden Birini Tutan Birleşimler” 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. Sabit Değer Kümesi İçin Enum'lar
  2. Birden Çok Türden Birini Tutan Birleşimler
  3. Etiketli Birleşimler ve switch
  4. Enum Yöntemleri ve Arka Plandaki Tam Sayılar
← Zig Academy Sayfasına Dön