Skip to content

Instantly share code, notes, and snippets.

@roshane
Last active May 24, 2022 01:26
Show Gist options
  • Save roshane/7a07d1b22a3a6aa2ca4427bf0c59a32a to your computer and use it in GitHub Desktop.
Save roshane/7a07d1b22a3a6aa2ca4427bf0c59a32a to your computer and use it in GitHub Desktop.
AutoCloseable resource with Scala
object ManagedResource {
def withResource[R <: java.io.Closeable, T](resource: R)(consumer: R => T) = {
try {
consumer(resource)
} finally {
resource.close
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment