0Pricing
Zig Academy · Ders

if ve orelse ile Güvenli Açma

null durumunu beklenmedik sonuçlar olmadan ele alın.

if ve orelse ile Güvenli Açma, 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.

You Must Unwrap First

An optional is not the value inside it. Before you can use the contents you have to unwrap it and deal with the null case.

Capture with if

An if on an optional can bind the inner value with a pipe capture. The body runs only when the optional is not null.

if (maybe) |value| {
    use(value);
}

Handle the null Branch

Add an else to react when the optional is null. Now both outcomes are covered with no chance of a missing case.

if (maybe) |v| {
    use(v);
} else {
    handleEmpty();
}

The Capture Is Unwrapped

Inside the if body the captured name is the plain inner type, not an optional. The question mark is already gone for you.

if (age) |a| {
    // a is i32, not ?i32
}

orelse Supplies a Default

The orelse operator unwraps an optional, but if it is null it gives back the value on its right instead.

const a = age orelse 0;

orelse Is an Expression

Because orelse returns a value, you can use it inline anywhere, like passing a defaulted number straight into a function call.

print(level orelse 1);

orelse Can Run Code

The right side of orelse may be a block. Use it to compute a fallback or even return early from the current function.

const v = lookup() orelse return;

Force Unwrap with .?

Writing .? asserts the optional is not null and hands back the value. If it is null in a safe build, your program panics.

const a = age.?;

Use .? Only When Certain

Reach for .? only when null is truly impossible. Otherwise prefer if or orelse so an empty value never crashes you.

Choose by Intent

Use if to branch on presence, orelse for a quick default, and .? only when you can prove the value is there.

Safety Is the Default

Each tool forces you to acknowledge null. That is why Zig optionals rarely turn into the surprise crashes seen in other languages.

Quick Check

You want a default of 0 when an optional integer is null. Which fits best?

Recap

You can unwrap optionals with if captures, supply defaults via orelse, or assert with .?. Each one makes you face null on purpose. ✅

Sıkça Sorulan Sorular

“if ve orelse ile Güvenli Açma” dersi ücretsiz mi?

Evet — “if ve orelse ile Güvenli Açma” 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.

“if ve orelse ile Güvenli Açma” dersinde ne öğreneceğim?

null durumunu beklenmedik sonuçlar olmadan ele alı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.

“if ve orelse ile Güvenli Açma” 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. İsteğe Bağlı Türleri Bildirme
  2. if ve orelse ile Güvenli Açma
  3. İsteğe Bağlı İşaretçiler ve ?*T
  4. null ve undefined Karşılaştırması
← Zig Academy Sayfasına Dön