Introducing Scopes in ZIO 2.0

Say Goodbye to ZManaged and Hello to Scope!

Pondering the implications

(Re)Working a Simple Example

val fileReader =
ZManaged.acquireReleaseAttemptWith(Source.fromFile("build.sbt"))
(_.close())
val getLineCount: ZIO[Any, Throwable, Int] = fileReader.use { f =>
Task.attemptBlocking(f.getLines().size())
}
val fileReader = ZManaged.fromAutoCloseable {
ZIO.attemptBlocking(Source.fromFile("build.sbt"))
}

Managed Resources with Layers

val fileLayer: ZLayer[Any, Throwable, Iterator[String] =
fileReader.map(_.getLines()).toLayer
val countLinesProgram: ZIO[Iterator[String], Throwable, Int] = for {
data <- ZIO.service[Iterator[String]]
size <- ZIO.attempt(data.size)
} yield size
override def run: ZIO[ZEnv, Any, Any] = {
for {
data <- fileReader.use(f =>
Task.attemptBlocking(f.getLines().size))
data2 <- countLinesProgram.provideLayer(fileLayer)
_ <- Console.printLine(s"$data, $data2")
} yield data
}

Shifting to “Scope”

val fileReader = 
ZIO.fromAutoCloseable(
ZIO.attemptBlockingIO(Source.fromFile("build.sbt")))
val read = fileReader.flatMap(f => 
ZIO.attemptBlockingIO(f.getLines().size))
val read = ZIO.scoped { 
fileReader.flatMap(f => ZIO.attemptBlockingIO(f.getLines().size))
}

Scoped Layers

val fileLayer: ZLayer[Scope, IOException, Iterator[String]] = 
fileReader.map(_.getLines()).toLayer
val fileLayer: ZLayer[Scope, IOException, Iterator[String]] =
ZLayer.fromZIO(fileReader.map(_.getLines()))
val countLinesProgram: ZIO[Iterator[String], IOException, Int] = for {
data <- ZIO.service[Iterator[String]]
size <- ZIO.attemptBlockingIO(data.size)
} yield size
val toRun: ZIO[Scope, IOException, Int] =
countLinesProgram.provideLayer(fileLayer)

Important: the better way to construct the ZLayer!

val fileLayer: ZLayer[Any, IOException, Iterator[String]] = 
ZLayer.scoped(fileReader.map(_.getLines())

Running the program

override def run: ZIO[Any, Throwable, Int] = {
for {
data <- fileReader.use(f => Task.attempt(f.getLines().size))
data2 <- countLinesProgram.provideLayer(fileLayer)
_ <- Console.printLine(s"$data, $data2")
} yield data
}
override def run: ZIO[Any, IOException, Int] = {
for {
data <- ZIO.scoped(fileReader.flatMap(f =>
Task.attemptBlockingIO(f.getLines().size)) )
data2 <- ZIO.scoped(countLinesProgram.provideLayer(fileLayer))
_ <- Console.printLine(s"$data, $data2")
} yield data
}
override def run: ZIO[Any, IOException, Int] = {
ZIO.scoped {
for {
data <- fileReader.flatMap(f =>
Task.attemptBlockingIO(f.getLines().size))
data2 <- countLinesProgram.provideLayer(fileLayer)
_ <- Console.printLine(s"$data, $data2")
} yield data
}
}
override def run: ZIO[Scope, IOException, Int] = {
for {
data <- fileReader.flatMap(f =>
Task.attemptBlockingIO(f.getLines().size))
data2 <- countLinesProgram.provideLayer(fileLayer)
_ <- Console.printLine(s"$data, $data2")
} yield data
}

In conclusion…

--

--

--

Life-long learner, foodie and wine enthusiast, living in Austin, Texas.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Rails rendering is slow — We use a Node microservice to achieve greater site performance

Create Own Custom Object Detection Model using TensorFlow and Deploy it to Android App

Rand (NumPy) and Randint (NumPy) in Python

Working with AWS CLI

Useful Programming Concepts for Everyday Life

How to Configure iTerm2 and Vim like a Pro on macOS

MQTT X Guideline

Testing all links in the chain

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Murray Todd Williams

Murray Todd Williams

Life-long learner, foodie and wine enthusiast, living in Austin, Texas.

More from Medium

Road to ZIO[n]: Chapter 4, the Persistence layer

The motivation of the actor model for distributed systems

Scala 3: “Erased” Definitions