0Pricing
Zig Academy · レッスン

ifとorelseで安全にアンラップする

予期しない動作を避けながらnullを処理します。

「ifとorelseで安全にアンラップする」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「ifとorelseで安全にアンラップする」レッスンは無料ですか?

はい。「ifとorelseで安全にアンラップする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。

「ifとorelseで安全にアンラップする」で何を学びますか?

予期しない動作を避けながらnullを処理します。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Zig Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「ifとorelseで安全にアンラップする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このZig Academyレッスンでコードを書いて実行できますか?

はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. オプショナル型を宣言する
  2. ifとorelseで安全にアンラップする
  3. オプショナルポインターと?*T
  4. nullとundefinedの違い
← Zig Academyに戻る