التعدادات لمجموعة قيم ثابتة
سمّوا قائمة مغلقة من الخيارات
التعدادات لمجموعة قيم ثابتة درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
A Closed List of Choices
Some values come from a small, fixed menu: a traffic light is red, yellow, or green. An enum names exactly that closed list of options.
Declaring an Enum
You write enum and list the names you want inside braces. Each name is one valid value of that type.
const Color = enum { red, green, blue };Enum Values Are Names
Each entry like red or blue is a real value of the type, not a string. You refer to it through the enum, keeping it tidy and typed.
const c: Color = Color.red;The Dot Shorthand
When Zig already knows the expected type, you can drop the name and just write a dot before the value.
const c: Color = .green;Comparing Enum Values
You compare enum values with == just like numbers. This makes branching on a fixed choice clean and readable.
if (c == .blue) {}Only Listed Values Are Allowed
An enum can hold only the names you declared. Try to use one that does not exist and the compiler rejects it right away.
const c: Color = .purple; // errorSelf-Documenting Code
Reading .green is far clearer than reading the number 1. Enums turn magic numbers into meaningful names you can trust.
Enums in Function Parameters
Accept an enum as a parameter and a caller can only pass one of your valid options, so bad input is caught at compile time.
fn paint(c: Color) void {}Switch Pairs Well with Enums
Enums shine with switch: you handle each named case, and Zig reminds you if you forget one of the options.
switch (c) { .red => {}, .green => {}, .blue => {} }Naming Conventions
Enum types use TitleCase like Color, while the individual values use lower snake_case like light_blue. This keeps the two roles distinct.
Why Reach for an Enum
Whenever a value is one of a few known states, an enum makes the choices explicit, safe, and impossible to misspell silently.
Quick Check
You want a type that is exactly one of red, green, or blue. What fits?
Recap
You met enums: a type that names a fixed set of values. Use the dot shorthand, compare with ==, and let the compiler block invalid choices. 🚦
الأسئلة الشائعة
هل درس «التعدادات لمجموعة قيم ثابتة» مجاني؟
نعم — نص درس «التعدادات لمجموعة قيم ثابتة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.
ماذا ستتعلم في «التعدادات لمجموعة قيم ثابتة»؟
سمّوا قائمة مغلقة من الخيارات تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟
لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «التعدادات لمجموعة قيم ثابتة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟
نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التعدادات لمجموعة قيم ثابتة
- الاتحادات التي تحتوي أحد أنواع متعددة
- الاتحادات الموسومة وswitch
- طرائق التعدادات والأعداد الصحيحة الداعمة