-
-
Notifications
You must be signed in to change notification settings - Fork 244
Open
Milestone
Description
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 implementationAnother 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
}
}.futureLiftMetadata
Metadata
Assignees
Labels
No labels