0Pricing
Zig Academy · درس

الاتحادات الموسومة وswitch

طابقوا الأنماط على متغيرات الاتحاد

الاتحادات الموسومة وswitch درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «الاتحادات الموسومة وswitch»؟

طابقوا الأنماط على متغيرات الاتحاد تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟

لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «الاتحادات الموسومة وswitch»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟

نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التعدادات لمجموعة قيم ثابتة
  2. الاتحادات التي تحتوي أحد أنواع متعددة
  3. الاتحادات الموسومة وswitch
  4. طرائق التعدادات والأعداد الصحيحة الداعمة
← العودة إلى Zig Academy