4 ms·
>It's funny. For all the useless Java "factory" patterns, the one thing there ISN'T a factory for - Futures[...]. >It's a bit of a puzzle to me why this isn't j
by JD557 6y ago
>It's funny. For all the useless Java "factory" patterns, the one thing there ISN'T a factory for - Futures[...].
>It's a bit of a puzzle to me why this isn't just standardized.
It kind of is. A `FutureFactory` it's pretty much `IO` (as in, Haskell's IO).
It just so happens that a lot of implementations don't want you to call `unsafeRun` (so that `unsafeRun` in only called at the edge of the world) and some implemetations don't like, but nothing stops you from having a `unsafeRunToFuture` that returns a `Future`. This is actually pretty common in Scala.
Some examples:
- Cats-effect: https://typelevel.org/cats-effect/datatypes/io.html#unsafetofuture https://typelevel.org/cats-effect/datatypes/io.html#unsafeto...
- ZIO: https://javadoc.io/doc/dev.zio/zio_2.12/latest/zio/Runtime.html#unsafeRunToFuture[E%3C:Throwable,A](zio:zio.ZIO[R,E,A]):zio.CancelableFuture[A] https://javadoc.io/doc/dev.zio/zio_2.12/latest/zio/Runtime.h...
- Monix: https://monix.io/api/current/monix/eval/Task.html#runToFuture(implicits:monix.execution.Scheduler):monix.execution.CancelableFuture[A] https://monix.io/api/current/monix/eval/Task.html#runToFutur...
I guess that this is not very standardized in OOP languages because they lack some of the ergonomics to use such factories (namely, do-notation).