Does anyone know if it’s possible to get KMP lambd...
# multiplatform
n
Does anyone know if it’s possible to get KMP lambda suspend functions working for ios? In KMP, I want to do something like:
Copy code
class ExampleClass<TRequest, TResponse> (private val suspendCall: suspend (TRequest) -> TResponse)
But in Swift/obj-c that gets compile to:
Copy code
public class ExampleClass<TRequest, TResponse> : KotlinBase where TRequest : AnyObject, TResponse : AnyObject {

    public init(suspendCall: any KotlinSuspendFunction1)
}
One workaround I thought of was instead of passing a lambda, I pass in an abstract class with that function, however Swift doesn’t support anonymous classes and I don’t want to have to define an explicit input class type each time I create the
ExampleClass
. I can’t use protocols here either because obj-c doesn’t support generics. If the
suspendCall
constructor param in KMP isn’t a suspend fun, then the lambda code in Swift is generated fine, but when I add the
suspend
keyword it’s not able to generate the async/await lambda. Does anyone have any ideas how to work around this?
m
Not an objective C developer but why does your Objective C compiled code not work?
n
The compiled obj-c code turns my suspend call lambda in
KotlinSuspendFunction1
which isn’t usable in swift (at least I don’t see how it can be used)
j
Nicole, it's likely not possible since it interop needs to go through Objective-C. You might experiment with some ways of doing it through a different pattern (like a SAM interface?) combined with SKIE.
n
Thanks! Yeah I was coming to a similar conclusion, but wasn’t sure if I was missing something.
108 Views