I've got a KMM framework with native stuff targeti...
# kotlin-native
s
I've got a KMM framework with native stuff targeting Mac and IOS built and am learning Swift-calling-Kotlin. I have a question about the experimental async/suspend support when calling suspend functions that have suspend lambdas from swift. Say I have a Kotlin function like this:
suspend fun anyname(arg1:String, block: suspend (arg) -> Unit)
. What is the best way to invoke this from swift? I'm watching some unresolved issues in Youtrack (https://youtrack.jetbrains.com/issue/KT-47610 ) on making this better, but with current swift and kotlin 1.6.10 am having trouble figuring out if there is a non-messy way to do this. Specifically the best way to code the
block
lambda above in the Swift call of
anyname("xxx") { arg -> ... }
. I'm currently playing with using a class that implements the kotlin-generated swift protocol
KotlinSuspendFunction1
which has an
invoke
function that is getting called with arg1: Any? and a completionHandler. Is that the best/only way to make this work, or am I going down a rabbit hole since I'm a Swift newbie? If anyone can point me to an example of doing this better, I'd appreciate it. I guess the real answer I'm asking for is whether this is even a good thing to try to do from Swift until it gets improved 🙂. Thanks in advance for any tips/links.
e
you can take some inspiration from https://github.com/touchlab/SwiftCoroutines; at the moment the best approach may be defining wrappers in Kotlin that aren't really idiomatic Kotlin but are there to allow for easier use from Swift, and Swift wrappers around those to further convert them into something usable from idiomatic Swift
s
Makes sense, thanks for the info and the speedy response!