Last active
February 3, 2026 20:23
-
-
Save dacr/9e5738163b7715c7f6536649f0463864 to your computer and use it in GitHub Desktop.
ZIO learning - ensuring finalization is run / published by https://github.com/dacr/code-examples-manager #140b1a02-f594-4723-aa75-43f3173d2e88/d2c27889ac5ed7f34ea4455b51905ff9d828f791
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : ZIO learning - ensuring finalization is run | |
| // keywords : scala, zio, learning, pure-functional, ensuring, finalizer, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 140b1a02-f594-4723-aa75-43f3173d2e88 | |
| // created-on : 2021-04-06T11:07:45+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "dev.zio::zio:2.0.13" | |
| //> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
| // --------------------- | |
| /* | |
| inspired from | |
| - [John A De Goes - ZIO: Next-Generation Effects in Scala](https://www.youtube.com/watch?v=mkSHhsJXjdc) | |
| - +24:30 | |
| */ | |
| import zio.*, zio.worksheet.* | |
| import java.nio.file.{Files, Path} | |
| val path = Path.of("/tmp", "truc") | |
| val logic = | |
| ZIO | |
| .attempt(Files.createFile(path)) | |
| .ensuring(ZIO.attempt(Files.delete(path)).catchAll(ex => ZIO.logError(ex.getMessage))) | |
| .ensuring(ZIO.logInfo("everything's completed")) | |
| // ------------------------------------------------------------- | |
| logic.unsafeRun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment