Would it make sense for `kotlinx-coroutines` to ex...
# coroutines
d
Would it make sense for
kotlinx-coroutines
to expose an
Async<T>
type alias? That'll resolve to
Promise<T>
in JS,
CompletableFuture<T>
in Java8,
ListenableFuture<T>
on Android and
Deferred<T>
on Native. (Basically an appropriate async type for each platform that can be easily consumed from their respective languages). Then a
GlobalScope.platformAsync
or something to go along with it. I find myself needing to do this already since suspension only works within Kotlin itself and not from Java, JS or Obj-C.
l
Wouldn't work well because of binary compatibility and libraries targeting JVM, not excluding Android. Plus cancellation support (offered by
Job
) is not included in these platform or library specific primitives. You can still do it yourself, and add the extra features like cancellation if you need them.
d
I should have mentioned I don't intend to actually use this type in code. It should only be used as a return type from Kotlin. The type alias will not have any features besides type substitution.
It's just a way to expose async primitives to other languages, Kotlin code shouldn't call it directly (maybe in unit tests but that's a different issue).
l
I think it'd be complicated to make it work right now, but probably doable as a plugin compiler once new backend IR is ready and allows for multiplatform plugin compilers.
d
Yeah that seems to be the solution.
l
I think the best solution right now is to make a top level function that transforms a suspending lambda into what you need as a callback type, or direct callback. Then, make a wrapper for these languages. And for MPP, you can expect actual it, and use typealiases if you want, not even needing to have the same callback style on all platforms as you can expect a class without details.
In other words, use that "actually expected" suspend function wrapper, and make a façade with it for non Kotlin languages.
d
Yeah, that's an option.