Skip to content

Instantly share code, notes, and snippets.

@frosforever
Created April 1, 2015 16:09
Show Gist options
  • Save frosforever/ec7b8b4ee6b59602fb22 to your computer and use it in GitHub Desktop.
Save frosforever/ec7b8b4ee6b59602fb22 to your computer and use it in GitHub Desktop.
Open, use, and clean up resource safely
def cleanly[T, V](create: => T)(cleanup: T => Unit)(f: T => V): Try[V] =
Try {
val t = create
try {
f(t)
} finally {
cleanup(t)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment