Kotlin Multiplatform, from the compiler up · check

Know One codebase, two worlds already?

One question for each of the 5 puzzles in this module. A right answer marks that puzzle as passed, so you can jump ahead. A wrong one tells you which puzzle to start with. Nothing is sent anywhere: it's saved in this browser.

Puzzle 0: One Kotlin, many machines

Your shared Kotlin already runs on Android and iOS. You add a desktop target. What happens to the shared code?

Not quite. The shared Kotlin is compiled again, by another back end. Nothing is rewritten. Learn it in puzzle 0 →

Right. One front end, one back end per target.

Not quite. There's one front end for every target; only the back end differs. Learn it in puzzle 0 →

Puzzle 1: What every target has

You call java.io.File in commonMain, and the project has Android and iOS targets. What happens when you build for iOS?

Not quite. Common code is compiled for every target; on iOS it can't compile at all. Learn it in puzzle 1 →

Not quite. Kotlin/Native compiles your Kotlin; it can't bring along a Java library the iPhone doesn't have. Learn it in puzzle 1 →

Right. Common code may only use what every target has.

Puzzle 2: A promise and its keepers

You add a web target. platformName() has actuals for Android and iOS only. What happens?

Not quite. There's no default: the compiler requires an actual for every target. Learn it in puzzle 2 →

Right. Every target must keep the promise.

Not quite. One platform's actual is never used for another; each target needs its own. Learn it in puzzle 2 →

Puzzle 3: How Swift sees your Kotlin

The iOS team opens your framework in Xcode. What do they read to learn your API?

Not quite. Kotlin describes your code in Objective-C, not Swift; Swift reads that. Learn it in puzzle 3 →

Not quite. The framework holds native code and an Objective-C description; Swift never reads Kotlin. Learn it in puzzle 3 →

Right. Swift learns your API from the Objective-C header.

Puzzle 4: Lost in translation

Your shared code exposes a Flow<User>. What does Swift see?

Not quite. Generics cross only on classes; Flow is an interface, so its type parameter is lost. Learn it in puzzle 4 →

Right. Interface generics are lost in the Objective-C header.

Not quite. The Flow does cross, as an Objective-C type; only its type parameter is lost. Learn it in puzzle 4 →

Back to the overview →