لماذا توجد expect/actual
آلية تنفيذ السلوك الخاص بالمنصة خلف API واحد
لماذا توجد expect/actual درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Codebase, Different Worlds
Most of your Kotlin runs everywhere, but some tasks need each platform's own tools. That gap is exactly what expect/actual was built to close.
The Problem It Solves
Getting the OS version, a file path, or current time relies on Android or iOS APIs that simply do not exist in shared code. You still want to call them from commonMain. 🤔
A Promise and a Fulfillment
Think of expect as a promise: common code declares an API exists. Then each platform fulfills that promise with a real implementation.
The expect Declaration
In commonMain you write a declaration with no body. The expect keyword means the implementation lives elsewhere, per platform.
expect fun platformName(): StringThe actual Implementation
Each target supplies the body with actual. Android returns one value, iOS returns another, but the signature must match exactly.
actual fun platformName(): String = "Android"Common Code Stays Common
Your shared logic just calls the function and never sees the difference. The compiler picks the right actual for each build.
val label = "Running on " + platformName()Not the Same as an Interface
An interface needs an object to implement it at runtime. expect/actual is resolved at compile time, so there is zero runtime cost.
Where Each Piece Lives
The expect goes in commonMain. Every actual lives in its matching source set, like androidMain or iosMain, side by side.
Every Target Must Answer
If you ship to Android and iOS, both need an actual. A missing one is a compile error, not a surprise crash later.
Use It Sparingly
Reach for expect/actual only for the thin layer that truly differs. The more code stays in common, the more you share. ✨
A Bridge, Not a Wall
expect/actual is a clean bridge to native power without splitting your project. You keep one API while each platform speaks its own language.
Quick Check
Let's make sure the core idea landed.
Recap
You learned why expect/actual exists: it lets common code call platform-specific behavior through one API, resolved at compile time with no runtime cost. 🎉
الأسئلة الشائعة
هل درس «لماذا توجد expect/actual» مجاني؟
نعم — نص درس «لماذا توجد expect/actual» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «لماذا توجد expect/actual»؟
آلية تنفيذ السلوك الخاص بالمنصة خلف API واحد تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «لماذا توجد expect/actual»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا توجد expect/actual
- الحصول على اسم المنصة لكل هدف
- فئات وخصائص expect/actual
- الأخطاء الشائعة وأخطاء المترجم