🐦 Kotlin Advanced

What is the expect/actual mechanism in Kotlin Multiplatform?

Answer

The expect/actual mechanism is Kotlin Multiplatform's way of declaring a platform-agnostic API in common code while providing platform-specific implementations. In commonMain, you write expect fun getPlatformName(): String — a declaration without an implementation. In androidMain, you write actual fun getPlatformName() = "Android ${Build.VERSION.SDK_INT}", and in iosMain, actual fun getPlatformName() = "iOS ${UIDevice.currentDevice.systemVersion}". The compiler enforces that every expect declaration has a corresponding actual on every target platform.