John O'Reilly
12/03/2024, 6:24 PM// Kotlin
fun myFunc(val someSwiftClosure: (kotlinFunc: (Int) -> Unit) -> Unit)
// Swift
myFunc(someSwiftClosure: { kotlinFunc in
kotlinFunc(1) // invoking here immediately as example but plan would be to store and invoke later
})
if however I try something like following for example then I get Cannot call value of non-function type 'any KotlinSuspendFunction1'
// Kotlin
fun myFunc(val someSwiftClosure: (kotlinFunc: suspend (Int) -> Unit) -> Unit)
// Swift
myFunc(someSwiftClosure: { kotlinFunc in
Task {
await kotlinFunc(1)
}
})
Tadeas Kriz
12/03/2024, 7:43 PMsuspend (Int) -> Unit
. For example:
data class SuspendFunction1Wrapper<P1: Any, Result: Any>(
private val function: (P1) -> Result,
) {
suspend fun invoke(p1: P1): Result = function(p1)
}
fun myFunc(someSwiftClosure: (kotlinFunc: SuspendFunction1Wrapper<Int, Unit>) -> Unit)
// Swift
myFunc { kotlinFunc in
Task {
try await kotlinFunc.invoke(p1: 1)
}
}
It's not perfect, but at this point I don't think there's a different way to go around it. Also keep in mind Task { ... }
in Swift breaks cooperative concurrency (it's the same as Kotlin's GlobalScope.launch { ... }
in this case).
I could see SKIE hiding the original implementation and replacing it with an extension function that'd replace KotlinSuspendFunction1
with a real async lambda, but that's essentially a new SKIE feature.John O'Reilly
12/03/2024, 7:45 PMTadeas Kriz
12/03/2024, 7:45 PMJohn O'Reilly
12/03/2024, 7:46 PMTadeas Kriz
12/03/2024, 7:46 PMTadeas Kriz
12/03/2024, 7:47 PMTadeas Kriz
12/03/2024, 7:47 PM.map
function is a good example.John O'Reilly
12/03/2024, 7:48 PMJohn O'Reilly
12/04/2024, 9:50 AM