https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
e

Euler

04/13/2020, 12:19 PM
Hi, everyone! Do you know when suspend functions in Kotlin will be supported in Swift? I am generating a framework using cinterop to import external Objective C framework into my project. The generated framework is used by an application developed in Swift. I have to follow a specification which describes the APIs of my framework. In the documentation, there are synchronous and asynchronous APIs. My problem is how to implement the APIs synchronous if suspend functions in Kotlin are not supported in Swift. https://kotlinlang.org/docs/reference/native/objc_interop.html#unsupported
r

russhwolf

04/13/2020, 12:51 PM
This merged recently, though I haven't checked to see if it's usable in 1.4-M1 or not: https://github.com/JetBrains/kotlin-native/pull/3915
👏 4
😍 1
e

Euler

04/13/2020, 2:09 PM
That is a great news!!! Thanks!!!
e

Euler

04/13/2020, 3:09 PM
It will be available as functions with 
completionHandler:
 callback. Does it mean that the function will be asynchronous?
k

Kweku

04/13/2020, 10:12 PM
How do you use the suspend functions from swift? With no coroutines?
e

Euler

04/14/2020, 10:46 AM
In Swift, this code will run the statements in a background thread. Is it possible to map Kotlin coroutines to this? DispatchQueue.global(qos: .background).async {}
k

Kweku

04/14/2020, 12:11 PM
Doesn't suspend mean that the function suspends a coroutine and therefore has to be called inside one?
e

Euler

04/14/2020, 6:20 PM
Yes. So, my idea is to use the same approach. When a function is declared suspend in Kotlin, it could mean in Swift that it must run in a background thread. In fact, there is not an equivalent statement in Swift. An idea is to not consider "suspend" in Swift. Just map it to a normal function instead of changing the function protocol. Let the developer deal with the fact that the call must be done in a background thread. Maybe, causing a runtime exception should be enough to warn the developer. Is it possible to implement this way?
12 Views