0Pricing
Zig Academy · درس

الاتحادات التي تحتوي أحد أنواع متعددة

خزّنوا أشكالاً بديلة في موضع واحد

الاتحادات التي تحتوي أحد أنواع متعددة درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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

الأسئلة الشائعة

هل درس «الاتحادات التي تحتوي أحد أنواع متعددة» مجاني؟

نعم — نص درس «الاتحادات التي تحتوي أحد أنواع متعددة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

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

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

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

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

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

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

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

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

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

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