This Kotlin compiler plugin is meant to generate Java friendly APIs based on suspend
code defined in commonMain
.
Drawing inspiration from several projects, and techniques out there.
class MyService : CoroutineScope by scope, AutoCloseable {
override val coroutineContext: CoroutineContext = Dispatchers.Default
@AsFuture
suspend fun example(): Int {
delay(1000)
return 42
}
override fun close() = runBlocking {
scope.cancelAndJoin()
}
}
This will generate a Future
based API for example
:
class App {
public static void main(String[] args) {
MyService service = new MyService();
service.example()
.thenAccept(System.out::println);
}
}
Whilst from Kotlin (Multiplatform) example
will remain our originally defined suspend
function.
This happens by applying following techniques:
- The
example
Java function is generated injvmMain
, and usesCoroutineScope
to launch afuture
. - The newly generated Java function is annotated with
@Deprecated("Only callable from Java", level = DeprecationLevel.HIDDEN)
such that we cannot see this function from the Kotlin API. - The original
example
function gets@JvmName
toexampleSuspend
.
This gives us a Java API that support the reactive nature of suspend
without having to rely on runBlocking
(which doesn't exist for JVM),
and we can still turn it into blocking using get
. Which for Project Loom is non-blocking.
By exposing Future
, we can also easily integrate with other languages on the JVM that support Future
(e.g. Scala).