تعليقات الأنواع المفيدة
أضف الأنواع لتمكين Mojo من التحسين واكتشاف الأخطاء
تعليقات الأنواع المفيدة درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is a Type Annotation?
A type annotation states a value's type right in the declaration, so both you and Mojo know exactly what it holds.
Annotating a var
Add a colon and the type after the name. Here : Int promises that count will only ever hold whole numbers.
var count: Int = 0Inference vs Annotation
Without an annotation Mojo infers the type from the value. Writing it yourself makes your intent explicit and clear.
var x = 5
var y: Int = 5Catching Mistakes Early
An annotation lets Mojo reject a wrong value at compile time. Assigning text to an Int becomes an immediate error.
var count: Int = "oops" # errorAnnotations Aid Speed
Knowing the exact type ahead of time, Mojo can generate faster machine code instead of checking types as it runs.
Annotating Floats and Bools
Annotations work for every type. Spell out Float64 or Bool to document precisely what each value is.
var ratio: Float64 = 0.5
var ok: Bool = TrueAnnotating Strings
Mark text values with String so readers instantly see that a var carries words rather than numbers.
var name: String = "Ada"Annotations as Documentation
A type next to a name tells the next reader, including future you, what to expect without hunting for the value.
Required in fn Functions
Strict fn functions require type annotations on their parameters, which is where this habit pays off most.
fn add(a: Int, b: Int):
print(a + b)Annotate at Boundaries
Even when inference works, annotate the values others rely on. Clear types at boundaries prevent confusion across your code.
Types Are a Safety Net
Annotations turn silent bugs into loud errors and unlock speed. A little typing now saves real debugging later.
Quick Check
You annotate a var as Int but try to assign it the text "oops". What happens in Mojo?
Recap
A type annotation names a value's type with a colon, catching errors early and helping Mojo run faster. 🎯
الأسئلة الشائعة
هل درس «تعليقات الأنواع المفيدة» مجاني؟
نعم — نص درس «تعليقات الأنواع المفيدة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعليقات الأنواع المفيدة»؟
أضف الأنواع لتمكين Mojo من التحسين واكتشاف الأخطاء تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تعليقات الأنواع المفيدة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التصريح بالقيم باستخدام var
- Int وFloat وBool
- السلاسل والنصوص
- تعليقات الأنواع المفيدة