0Pricing
Kotlin Multiplatform Academy · Lesson

Why expect/actual Exists

The mechanism for platform-specific behavior behind one API.

Why expect/actual Exists is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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(): String

The 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. 🎉

Frequently asked questions

Is the “Why expect/actual Exists” lesson free?

Yes — the full text of “Why expect/actual Exists” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why expect/actual Exists”?

The mechanism for platform-specific behavior behind one API. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why expect/actual Exists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why expect/actual Exists
  2. Get the Platform Name per Target
  3. expect/actual Classes & Properties
  4. Common Mistakes & Compiler Errors
← Back to Kotlin Multiplatform Academy