Junior — Middle
Can you describe the main differences and purposes of the key constructs 'expect' and 'actual' in testing?
sobes.tech AI
Answer from AI
The key constructs expect and actual are used in Kotlin Multiplatform to implement platform-dependent code.
expectdeclares an interface, class, function, or property without implementation — that is, it sets a contract that must be implemented on each target platform.actualprovides a specific implementation for a platform (Android, iOS, JVM, etc.).
In the context of testing, this allows writing common test code with expect declarations, and implementing actual versions for each platform, which simplifies cross-platform testing.
Example:
// commonMain
expect fun getPlatformName(): String
// androidMain
actual fun getPlatformName() = "Android"
// iosMain
actual fun getPlatformName() = "iOS"
Thus, expect/actual help separate platform-dependent logic and enable writing tests that work on all platforms.