Created
June 15, 2018 12:19
-
-
Save sebastianvoss/6de91c81b37aa2c8aea09717eafccb21 to your computer and use it in GitHub Desktop.
Using Cats bracket for resource handling.
This file contains 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
package com.sebastianvoss | |
import java.util.UUID | |
import cats.effect.{Bracket, IO} | |
object CatsEffectBracket extends App { | |
case class SomeResource(id: UUID) | |
val aquire = IO { | |
println("Acquiring resource") | |
SomeResource(UUID.randomUUID()) | |
} | |
val doStuff: SomeResource => IO[String] = resource => | |
IO { | |
println(s"Doing stuff with $resource") | |
throw new Exception("Bad thing") | |
//"Some result" | |
} | |
val release: SomeResource => IO[Unit] = resource => IO(println(s"Closing $resource")) | |
val p = Bracket[IO, Throwable].bracket(aquire)(doStuff)(release) | |
p.unsafeRunSync() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment