Why Kotlin Multiplatform's expect/actual Is Overused and What to Do Instead

A software engineer writing for DEV Community argues that expect/actual, Kotlin Multiplatform's flagship feature for bridging common and platform-specific code, is widely misused as a default pattern by new teams. The mechanism creates compile-time, fixed bindings between common declarations and platform implementations, making it impossible to inject fakes or run unit tests in commonTest without workarounds. Drawing on experience from a project called PaymentsLab, the author found that heavy reliance on expect/actual made payment use cases untestable because no fake implementations could be substituted. The recommended alternative is to define interfaces in common code and inject platform-specific implementations via dependency injection, enabling straightforward unit testing with fakes. The author suggests reserving expect/actual only for narrow, permanent platform joints — such as type aliases for platform types — where swapping implementations is never needed.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in