0Pricing
Zig Academy · レッスン

タグ付きユニオンとswitch

ユニオンのバリアントをパターンマッチします。

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

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

Remembering the Variant

A plain union forgets which field is active. A tagged union pairs the value with an enum tag that records the current variant.

Declaring a Tagged Union

You write union(enum) to let Zig generate a matching tag automatically, one tag name per field.

const Value = union(enum) { int: i64, text: []const u8 };

The Tag Tracks the Field

Now the value carries its own tag, like .int or .text. The union always knows which variant it currently holds.

var v = Value{ .int = 7 };

Switch over the Variants

Use switch on a tagged union to branch by variant. Each case names one tag and handles that shape of the value.

switch (v) { .int => {}, .text => {} }

Capture the Payload

Add a capture with vertical bars to pull out the inner value of the active field, ready to use inside that branch.

switch (v) { .int => |n| useInt(n), .text => |s| useText(s) }

Exhaustiveness Is Enforced

Zig requires every variant to be handled. Forget a case and the compiler stops you, so you never silently miss one.

The else Branch

When you want to handle the rest in one place, add an else branch to cover any variants you did not list explicitly.

switch (v) { .int => |n| useInt(n), else => {} }

switch Is an Expression

A switch can produce a value, so each branch returns something. You assign the whole switch to a variable in one step.

const kind = switch (v) { .int => 1, .text => 2 };

Mutate Through a Capture

Capture by pointer with |*field| when you want to change the payload in place rather than copy it out of the union.

switch (v) { .int => |*n| n.* += 1, else => {} }

A Type-Safe Sum Type

Tagged unions are Zig's safe sum types: one value, many shapes, and a switch that the compiler proves you handled completely.

Perfect for Parsers and States

This pattern fits tokens, AST nodes, and state machines beautifully: model each case once and let switch route the logic.

Quick Check

What does writing union(enum) give you over a plain union?

Recap

You met tagged unions: union(enum) tracks the active variant. Switch over it, capture payloads with bars, and the compiler enforces every case. 🧩

よくある質問

「タグ付きユニオンとswitch」レッスンは無料ですか?

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

「タグ付きユニオンとswitch」で何を学びますか?

ユニオンのバリアントをパターンマッチします。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「タグ付きユニオンとswitch」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. 値の固定された集合にenumを使う
  2. 複数の型のいずれかを保持するユニオン
  3. タグ付きユニオンとswitch
  4. enumのメソッドと基底整数型
← Zig Academyに戻る