Thanks to visit codestin.com
Credit goes to github.com

Skip to content

monix-catnap: [Feature Proposal] Add a conversion to Java's CompletableFuture #1174

@satorg

Description

@satorg

According to this conversation, it could be handy to have an ability to convert backward from F[A] to CompletableFuture[A] (as the opposite conversion to what FutureLift provides).

Since CompletableFuture itself is impure, I think the result should be also enclosed into F[_], i.e. something like:

object FutureUnlift { // this name is controversial
  def from[F[_], Future[_], A](fa: F[A])(implicit F: FutureUnlift[F, Future]): F[Future[A]]
}

NOTE: an Executor instance might be required for such conversion.

For example, if there's some async Java method that accepts CompletableFuture:

// some async Java method
static <A, B> CompletableFuture<B> doSomethingAsync(CompletableFuture<A> future) { ... }

then it should be possible to achieve something like:

def callJavaAsyncMethod[F[_], A, B](a: F[A])(implicit F: Async[F]): F[B] = {
  FutureUnlift.from(a).flatMap { future: CompletableFuture[A] =>
    F.delay { doSomethingAsync(future) }
  }.futureLift
}

Also it could be nice to add an extension method to IO to let triggering its execution as CompletableFuture:

  // similarly to `unsafeToFuture`
  def unsafeToCompletableFuture: CompletableFuture[A] =
     FutureUnlift.from(io).unsafeRunSync // a naive implementation

Another use case: call a Java method with async callback:

<K, V> CompletableFuture<V> getOrLoad(K key, Supplier<CompletableFuture<V>> supplier);

then calling it from pure code could look like:

  def pureLoadData(key: Key): F[Data]

  def pureGetOrLoadData(key: Key): F[Data] = F.delay {
    getOrLoad { () =>
      pureLoadData(key).toIO.unsafeToCompletableFuture
    }
  }.futureLift

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions