expect and actual
Platform-specific implementations.
expect and actual is a free Kotlin Academy lesson on CoddyKit — lesson 2 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Need for Platform Code
Some functionality has no common implementation — getting the OS name, a UUID, or the current platform. KMM solves this with the expect/actual mechanism.
expect Declarations
In commonMain you declare an expect function or class — a contract with no body. Common code can call it.
// commonMain
expect fun platformName(): Stringactual on Android
Each platform provides an actual implementation matching the signature.
// androidMain
actual fun platformName(): String = "Android " + android.os.Build.VERSION.SDK_INTactual on iOS
The iOS source set supplies its own version, using native APIs.
// iosMain
import platform.UIKit.UIDevice
actual fun platformName(): String =
UIDevice.currentDevice.systemName + " " + UIDevice.currentDevice.systemVersionUsing It in Common Code
Common code calls the expected function as if it existed; the compiler links the right actual per target.
// commonMain
class Platform {
val name: String get() = platformName()
}expect Classes
You can expect an entire class. Each platform provides an actual class with matching members.
// commonMain
expect class FileReader(path: String) {
fun read(): String
}Actual Class Implementations
Platforms implement the expected class using native file APIs (e.g. Java IO on Android, Foundation on iOS).
// androidMain
actual class FileReader actual constructor(private val path: String) {
actual fun read(): String = java.io.File(path).readText()
}expect with typealias
An actual can be a typealias to an existing platform type, avoiding a wrapper.
// commonMain
expect class Uuid
// jvm/androidMain
actual typealias Uuid = java.util.UUIDSignature Matching Rules
The actual must match the expect exactly: same name, parameters, and return type. A mismatch is a compile error, guaranteeing consistency across platforms.
When Not to Use expect/actual
Prefer simple interfaces with platform implementations injected at runtime when the abstraction is more flexible. Reserve expect/actual for small, fixed platform primitives.
A Common-Code Illustration
Outside KMM, the same idea is just calling an abstraction. Here a plain function stands in for the platform call.
fun platformName(): String = "Demo Platform"
fun main() {
println("Running on " + platformName())
}Quick Check
Where is an expect declaration written, and where are its actual implementations?
Recap
expect/actual bridges platform gaps:
expectdeclares the contract incommonMainactualimplements it per platform (or viatypealias)- Signatures must match exactly
- Prefer interfaces for flexible abstractions
Next: sharing business logic.
Frequently asked questions
Is the “expect and actual” lesson free?
Yes — the full text of “expect and actual” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.
What will I learn in “expect and actual”?
Platform-specific implementations. You practise Kotlin 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 Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “expect and actual” 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 Academy lesson?
Yes. Every Kotlin 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
- KMM Project Structure
- expect and actual
- Sharing Business Logic
- Platform APIs