kierans777
02/15/2022, 5:55 AMsuspend
functions? For example if I have a Kotlin function
suspend fun doSomething(something: suspend (String) -> String): String {
...
something("hello")
...
return "world"
}
I want to write a function in Swift and pass it to the Kotlin function ie:
let swiftSomething = // create a Swift function
ModuleKt.doSometthing(swiftSomething)
However I'm not sure if that sort of idea is possible, or how to go about implementing it.Rick Clephas
02/15/2022, 7:41 AMkierans777
02/15/2022, 9:25 AMWhat would be a good use case for this?The use case is where you have a library that relies on some async work, that is platform dependent (eg: JVM/Android and iOS). Maybe some I/O or a computation you want to get off the main thread. However the library (in my very strong opinion) shouldn't force dependencies on the consuming app/platform. For example, if the async work is a HTTP request, and an Android app uses Retrofit, by using my library the app shouldn't be forced to include/bundle OkHttp, because, I as a library author chose to couple my library's unit of work (make a HTTP request) to a particular library/implementation of the unit of work. As an app developer this problem annoys me a lot as it leads to bloated builds, potential conflicts and it's a sign of bad abstraction. It's worse IMO on iOS since there's lack of namespacing (you might disagree) The proper way to solve this is to to create an abstraction and allow app developers to plugin the implementation that fits their need. For example (I use Arrow in my library) I can create a function signature
suspend (in: LibraryInput) -> Either<LibraryError, LibraryResult>
then app developers can plug in any function that matches the signature. Thus we get dependency inversion, etc.
This works really well in Kotlin and I have the main library with all the logic, and I've abstracted away the infrastructure dependencies (the whole Clean Architecture idea). My library is consumable in any JVM app with no infrastructure dependencies forced onto the consuming application.
However when cross compiling to iOS I'm now faced with the problem of providing implementations of the function types in Swift code in a way that conforms to Kotlin's "threading model". Not being as familiar with iOS development and new to MPP I'm not seeing an easy way to achieve what I want (if it's possible).
Hope that makes sense 🙂Rick Clephas
02/15/2022, 11:36 AM