الترجمة الشرطية
أنشئ فروع مسارات التعليمات البرمجية أثناء الترجمة
الترجمة الشرطية درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Choosing Code at Compile Time
Conditional compilation picks which code path to build based on compile-time values, so the unused branch never reaches the final program. ⚙️
The @parameter if Statement
Add @parameter to an if whose condition is known at compile time, and Mojo keeps only the branch that matches.
@parameter
if width == 1:
scalar_path()
else:
vector_path()The Condition Must Be Static
The test has to use parameters or aliases, values the compiler already knows, not data that only appears while the program runs.
alias DEBUG = False
@parameter
if DEBUG:
print("tracing")Dead Branches Disappear
The branch that fails the condition is simply not compiled, so it adds no size and costs nothing at runtime.
Specialize by Type
Use it to switch logic based on a type parameter, choosing the best implementation for floats versus integers, for example.
@parameter
if T.is_floating_point():
use_fma()
else:
use_int_mul()Branch on Hardware Facts
Pair conditional compilation with platform checks to emit the right kernel for the CPU or accelerator you are targeting.
Feature Flags Without Cost
Gate optional features behind an alias flag; when it is False the whole feature compiles away, leaving a lean binary.
Different From a Runtime if
A normal if always keeps both branches in the binary and tests at runtime; @parameter if decides once, at build time, and drops the rest.
Combine With Other Metaprogramming
Conditional compilation mixes well with parameterized functions and unrolled loops, letting each specialization take its own optimal path.
One Source, Many Targets
From a single definition you generate tailored builds for each platform or type, with no runtime branching tax to pay. 🚀
Keep Both Paths Valid
Each branch must still compile on its own when chosen, so write every path to be correct for the parameter values that select it.
Quick Check
Compare @parameter if with a normal if.
Recap
@parameter if chooses code paths at compile time: dead branches vanish, you specialize by type or platform, and the binary stays lean. 🎯
الأسئلة الشائعة
هل درس «الترجمة الشرطية» مجاني؟
نعم — نص درس «الترجمة الشرطية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «الترجمة الشرطية»؟
أنشئ فروع مسارات التعليمات البرمجية أثناء الترجمة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «الترجمة الشرطية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.