Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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/a25f73bc70bc55df6179d4bd007beb7d to your computer and use it in GitHub Desktop.
Save dacr/a25f73bc70bc55df6179d4bd007beb7d to your computer and use it in GitHub Desktop.
ZIO learning - managed resource using acquireReleaseAttemptWith approach / published by https://github.com/dacr/code-examples-manager #b8ff93de-5fbc-40a6-aacb-4a7efe393eb9/e0472e0561450a6f7c675bf11027c9583e2eff9c
// summary : ZIO learning - managed resource using acquireReleaseAttemptWith approach
// keywords : scala, zio, learning, pure-functional, resources, acquire, auto-release, @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 : b8ff93de-5fbc-40a6-aacb-4a7efe393eb9
// created-on : 2021-04-02T15:36:11+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"
// ---------------------
import zio.*, zio.worksheet.*
case class SomeResource() {
def read(): String = "SomeData"
def close(): Unit = {}
}
val managedResource = ZIO.acquireReleaseWith(ZIO.attempt(SomeResource()))(res => ZIO.succeed(res.close()))
val logic = managedResource { resource =>
Console.printLine(resource.read())
}
// -------------------------------------------------------------
logic.unsafeRun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment