Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dacr/a25f73bc70bc55df6179d4bd007beb7d to your computer and use it in GitHub Desktop.

Select an option

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/7e358693cc8929ba1fa968bda7c07bc5614e7634
// 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 License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// 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.4.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