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

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/overview/handling_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ val groupedFileData: IO[IOException, Unit] =

Like `ensuring`, `acquireReleaseWith` has compositional semantics, so if one `acquireReleaseWith` is nested inside another `acquireReleaseWith`, and the outer resource is acquired, then the outer release will always be called, even if, for example, the inner release fails.

For resources which implement the AutoClosable interface, the convenience method `fromAutoClosable` can be used, which can be seen as the ZIO equivalent of try-with-resource.

```scala mdoc:invisible
import zio._
import java.io.FileInputStream
def openFileInputStream(name: String): IO[Throwable, FileInputStream] = ZIO.attemptBlocking(new FileInputStream(name))
```

```scala mdoc:silent
val bytesInFile: IO[Throwable, Int] =
ZIO.scoped {
for {
stream <- ZIO.fromAutoCloseable(openFileInputStream("data.json"))
data <- ZIO.attemptBlockingIO(stream.readAllBytes())
} yield data.length
}
```

## Next Steps

If you are comfortable with basic resource handling, the next step is to learn about [basic concurrency](basic_concurrency.md).