https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Dominaezzz

08/26/2019, 3:41 PM
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

louiscad

08/26/2019, 4:11 PM
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

Dominaezzz

08/26/2019, 4:17 PM
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

louiscad

08/26/2019, 4:18 PM
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

Dominaezzz

08/26/2019, 4:19 PM
Yeah that seems to be the solution.
l

louiscad

08/26/2019, 4:21 PM
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

Dominaezzz

08/26/2019, 4:25 PM
Yeah, that's an option.
4 Views