Last active
May 25, 2024 10:18
-
-
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/5153798fde1c930470b6517d202751ee3698af1f
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 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.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