Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/9e5738163b7715c7f6536649f0463864 to your computer and use it in GitHub Desktop.
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/db83de8b59a7cd458f1e33190567f8e55c30d50d
// summary : ZIO learning - ensuring finalization is run
// keywords : scala, zio, learning, pure-functional, ensuring, finalizer, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.2.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