zio-interop-future


License

License

GroupId

GroupId

dev.zio
ArtifactId

ArtifactId

zio-interop-future_2.13
Last Version

Last Version

2.12.8.0-RC6
Release Date

Release Date

Type

Type

jar
Description

Description

zio-interop-future
zio-interop-future
Project URL

Project URL

https://zio.dev
Project Organization

Project Organization

dev.zio
Source Code Management

Source Code Management

https://github.com/zio/interop-future/

Download zio-interop-future_2.13

How to add to project

<!-- https://jarcasting.com/artifacts/dev.zio/zio-interop-future_2.13/ -->
<dependency>
    <groupId>dev.zio</groupId>
    <artifactId>zio-interop-future_2.13</artifactId>
    <version>2.12.8.0-RC6</version>
</dependency>
// https://jarcasting.com/artifacts/dev.zio/zio-interop-future_2.13/
implementation 'dev.zio:zio-interop-future_2.13:2.12.8.0-RC6'
// https://jarcasting.com/artifacts/dev.zio/zio-interop-future_2.13/
implementation ("dev.zio:zio-interop-future_2.13:2.12.8.0-RC6")
'dev.zio:zio-interop-future_2.13:jar:2.12.8.0-RC6'
<dependency org="dev.zio" name="zio-interop-future_2.13" rev="2.12.8.0-RC6">
  <artifact name="zio-interop-future_2.13" type="jar" />
</dependency>
@Grapes(
@Grab(group='dev.zio', module='zio-interop-future_2.13', version='2.12.8.0-RC6')
)
libraryDependencies += "dev.zio" % "zio-interop-future_2.13" % "2.12.8.0-RC6"
[dev.zio/zio-interop-future_2.13 "2.12.8.0-RC6"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.13.0
dev.zio : zio_2.13 jar 1.0.0-RC16

provided (1)

Group / Artifact Type Version
com.github.ghik : silencer-lib_2.13 jar 1.4.2

test (1)

Group / Artifact Type Version
org.scalacheck : scalacheck_2.13 jar 1.14.2

Project Modules

There are no modules declared in this project.

Interop Future

CircleCI

This library provides an interoperability layer with Scala's Future.

From Future

This is the extension method added to IO companion object:

def fromFuture[A](ftr: () => Future[A])(ec: ExecutionContext): Task[A] =

There are a few things to clarify here:

  • ftr, the expression producing the Future value, is a thunk (or Function0). The reason for that is, Future is eager, it means as soon as you call Future.apply the effect has started performing, that's not a desired behavior in Pure FP (which ZIO encourages). So it's recommended to declare expressions creating Futures using def instead of val.
  • Also you have to be explicit on which EC you want to use, having it implicit, as in the standard library, is a bad practice.
  • Finally, as you can see, the IO returned fixes the error type to Throwable since that's the only possible cause for a failed Future.

Example

// EC is not implicit
val myEC: ExecutionContext = ...

// future defined in thunk using def
def myFuture: Future[ALotOfData] = myLegacyHeavyJobReturningFuture(...)
val myIO: Task[ALotOfData] = IO.fromFuture(myFuture _)(myEC)

To Future

This extension method is added to values of type Task[A]:

def toFuture: UIO[Future[A]]

Notice that we don't actually return a Future but an infallible IO producing the Future when it's performed, that's again because as soon as we have a Future in our hands, whatever it does is already happening.

As an alternative, a more flexible extension method is added to any IO[E, A] to convert to Future as long as you can provide a function to convert from E to Throwable.

def toFutureE(f: E => Throwable): UIO[Future[A]]

Example

val safeFuture: UIO[Future[MoarData]] = myShinyNewApiBasedOnZio(...).toFuture(MyError.toThrowable)
val itsHappening: Future[MoarData] = unsafeRun(safeFuture)
dev.zio

ZIO

ZIO — Real World Functional Programming

Versions

Version
2.12.8.0-RC6
2.12.8.0-RC5
2.12.8.0-RC4