Last active
May 25, 2024 10:19
-
-
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/99dd6b10a1214a24c21b04c1e867182929a14053
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 - 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.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