Skip to content

Instantly share code, notes, and snippets.

@sebastianvoss
Created June 15, 2018 12:19
Show Gist options
  • Save sebastianvoss/6de91c81b37aa2c8aea09717eafccb21 to your computer and use it in GitHub Desktop.
Save sebastianvoss/6de91c81b37aa2c8aea09717eafccb21 to your computer and use it in GitHub Desktop.
Using Cats bracket for resource handling.
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