Code Monkey home page Code Monkey logo

asfuture's Introduction

AsFuture Compiler plugin

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.

Usage

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:

  1. The example Java function is generated in jvmMain, and uses CoroutineScope to launch a future.
  2. 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.
  3. The original 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).

asfuture's People

Contributors

nomisrev avatar dependabot[bot] avatar raulraja avatar

Stargazers

Bogdan Cordier avatar Pavel Ivanov avatar Petrus Nguyễn Thái Học avatar

Watchers

Nick Elsberry avatar  avatar Fede Fernández avatar Tomás Cayuelas Ruiz avatar Francisco Diaz avatar Ana Mª Marquez avatar Jose Gutiérrez de Ory avatar Juan Pedro Moreno avatar Javier Segovia Córdoba avatar Todd L Smith avatar Juan Valencia avatar David Vega Lichacz avatar Manuel MC avatar Daniel Ivan Gelvez Leon avatar Gerson Pozo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.