
Generates Java-friendly APIs from suspend functions, creating Future-based interfaces for seamless integration with Java and other JVM languages, while retaining original suspend functionality.
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:
example Java function is generated in jvmMain, and uses CoroutineScope to launch a future.@Deprecated("Only callable from Java", level = DeprecationLevel.HIDDEN) such that we cannot see this function from the Kotlin API.example function gets @JvmName to exampleSuspend.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).
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:
example Java function is generated in jvmMain, and uses CoroutineScope to launch a future.@Deprecated("Only callable from Java", level = DeprecationLevel.HIDDEN) such that we cannot see this function from the Kotlin API.example function gets @JvmName to exampleSuspend.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).