เหตุผลที่มี expect/actual
กลไกสำหรับพฤติกรรมเฉพาะแพลตฟอร์มภายใต้ API เดียว
เหตุผลที่มี expect/actual เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เหตุผลที่มี expect/actual”
กลไกสำหรับพฤติกรรมเฉพาะแพลตฟอร์มภายใต้ API เดียว คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เหตุผลที่มี expect/actual” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุผลที่มี expect/actual
- รับชื่อแพลตฟอร์มสำหรับแต่ละเป้าหมาย
- คลาสและพร็อพเพอร์ตี expect/actual
- ข้อผิดพลาดทั่วไปและข้อผิดพลาดของคอมไพเลอร์