Kotlin-native: Suggestion: export all suspend functions to iOS as return type Deferred<T> on Dispatchers.Default by default

Created on 4 Dec 2018  路  4Comments  路  Source: JetBrains/kotlin-native

Hi,
we are doing multi-platform mobile application and noticed that on every class which uses suspend functions we need to add new function fun xxxAsync():Deferred<X> = Global.async { xxx() } to be able to use that functions on iOS, and thought maybe this could be done automatically when exporting to iOS?
Could be possible that compiler emit fun xxx():Deferred<X> automatically when exporting to iOS? (could be with or without Async)? Maybe can be added some annotations which explicitly says on which dispatcher it should work.
I don't know if it will be good to have this by default in compiler, but if it's possible to do that by some compiler plugin, or compiler flag, would be great. Or do you have any recommendations on how to solve this, or any other plans how suspend functions can be supported in iOS?

Most helpful comment

Hi.
We consider exporting suspend functions to iOS quite important feature. But doing it right, as a feature of interoperability between Kotlin and Swift/Objective-C languages, requires some non-trivial preparations including proper design.

Implementing a compiler plugin for this seems possible and makes sense.

All 4 comments

Hi.
We consider exporting suspend functions to iOS quite important feature. But doing it right, as a feature of interoperability between Kotlin and Swift/Objective-C languages, requires some non-trivial preparations including proper design.

Implementing a compiler plugin for this seems possible and makes sense.

Could you give any guidelines or urls how to start writing a compiler plugin for this feature? (autogeneration "xxxAsync" for every public suspending "xxx" function)

Compiler plugins API for Kotlin/Native is neither stable nor ready yet, so it may appear to be tricker than expected.

There is a compiler plugin for kotlinx.serialization that can be used as an example.
Here is entry point of Kotlin/Native part: https://github.com/JetBrains/kotlin/blob/b611facd7149e53532a5992edffc3fd28094d199/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/extensions/SerializationLoweringExtension.kt
Building: https://github.com/JetBrains/kotlin/tree/master/plugins/kotlin-serialization/kotlin-serialization-compiler#building-and-usage

In Kotlin 1.4 (starting from M2) suspend functions will be available from Swift/Objective-C as functions with completionHandler: callback.
This will make it possible to implement a wrapper to make Deferred from Kotlin suspend fun, e.g.

kotlinDeferred { suspendFun(args, completionHandler: $0) }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SBNTT picture SBNTT  路  4Comments

brettwillis picture brettwillis  路  4Comments

msink picture msink  路  4Comments

AregevDev picture AregevDev  路  3Comments

alusch picture alusch  路  4Comments