Kotlin Multiplatform, from the compiler up · Puzzle 6 · 7 of 16
Lost in translation
In puzzle 5, Swift saw your Kotlin through an Objective-C header. Objective-C is simpler than both languages, so some things don't make it across. Here are the three that bite.
The problem
In puzzle 5, Swift reached your Kotlin through an Objective-C header. Your shared code returns a Flow<User>, has suspend functions, and throws an IOException when the login fails. Objective-C is older and simpler than both Kotlin and Swift. What happens to those three on the way?
See it
Everything Swift sees of your Kotlin passes through the Objective-C header from puzzle 5.
Step 1 of 4A
Flow<User>crosses, but itsUserfalls off: Objective-C keeps type parameters on classes only, and Flow is an interface.Step 2 of 4A
suspendfunction becomes a method with a completion handler: "call me back when done". Swift calls it as an async function, with await. Predict: what about an exception?Step 3 of 4An exception crosses only if the function lists it with
@Throws: then Swift gets an error it can catch. Without it, the app ends.Step 4 of 4
The building block
Generics. Objective-C allows type parameters only on classes, not on interfaces. Kotlin's Flow is an interface, so Flow<User> arrives in Swift without its User: Swift sees "some Flow" and has to be told what's inside. A class like Box<User> keeps its User.
suspend functions. Objective-C has no coroutines. Its way of saying "later" is a completion handler: a function you pass in, which is called with the result when it's ready. So each suspend function becomes a method that takes a completion handler. Swift has its own version of suspend: an async function, which the caller waits for with await, without blocking, much as a coroutine waits at a suspend call. Swift turns a method with a completion handler into an async function by itself, so the iOS team calls yours with await.
Exceptions. Every Kotlin exception is unchecked: nothing forces the caller to handle it. Swift's errors are checked: a function that can fail says so with throws, and the caller must handle it. So Kotlin has to say which exceptions may cross: you list them with @Throws, and those arrive in Swift as errors it can catch. Any other Kotlin exception that reaches Swift ends the app.
None of this is a bug: it's the price of the bridge from puzzle 5. Knowing it lets you shape your API so Swift gets what it needs: generic classes instead of generic interfaces, and @Throws on every function that can fail.
The analogy
The translation from puzzle 5 again. A word the third language doesn't have (a Kotlin interface's type parameter) is dropped. An idiom it can't express ("suspend") is rewritten into a phrase it has ("call me back"). And a warning nobody translated (an unlisted exception) reaches the reader as silence, then a crash.
Check yourself
Sources
- Kotlin docs: Interoperability with Swift/Objective-C ("Generics can only be defined on classes, not on interfaces (protocols in Objective-C and Swift) or functions"; mapping table: "suspend → completionHandler: / async"; "All Kotlin exceptions are unchecked… Swift has only checked errors"; "Other Kotlin exceptions reaching Swift/Objective-C are considered unhandled and cause program termination")
What you now know
Through the Objective-C header, generics on interfaces are lost (a Flow<User> arrives without its User), suspend functions become completion handlers (async in Swift), and a Kotlin exception not listed in @Throws ends the app.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.