Kotlin Multiplatform, from the compiler up · Puzzle 4 · 5 of 10

Lost in translation

In puzzle 3, 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 3, 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

Three things cross the Objective-C header: a Flow of User loses its User; a suspend function becomes a completion handler; an exception not listed in Throws ends the app Objective-C headerKotlinSwift Flow<User>Flow User suspend fun login()login(completion) throws, no @Throwsapp endswith @Throwserror Swift can catch
  1. Everything Swift sees of your Kotlin passes through the Objective-C header from puzzle 3.

    Step 1 of 4
  2. A Flow<User> crosses, but its User falls off: Objective-C keeps type parameters on classes only, and Flow is an interface.

    Step 2 of 4
  3. A suspend function becomes a method with a completion handler: "call me back when done". Swift calls it with await. Predict: what about an exception?

    Step 3 of 4
  4. An 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. 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 3. 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 3 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

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.

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.

A Kotlin function throws IOException and has no @Throws. Swift calls it, and it throws. What happens?

Right. Only listed exceptions become catchable errors.

Not quite. Nothing catches it on the way; an unlisted exception ends the app.

Not quite. Only exceptions listed in @Throws become Swift errors; the rest end the app.

What's the difference between a suspend function in Kotlin and what Swift sees?

Right. Suspend becomes "call me back when done".

Not quite. Nothing blocks: the completion handler is called later, with the result.

Not quite. Swift has no suspend; Objective-C's bridge turns it into a completion handler.

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.