withResource()
needs an implicit evidence parameter of this type
to know how to release what's passed to it.
Companion object for CanReleaseResource
, providing predefined implicit
evidence parameters for withResource()
.
Ensure that a closeable object is closed.
Ensure that a closeable object is closed. Note that this function
requires an implicit evidence parameter of type CanClose
to determine
how to close the object. You can implement your own, though common
ones are provided automatically.
Sample use:
withResource(new java.io.FileInputStream("/path/to/file")) { in => ... }
the object that holds a resource to be released
the code block to execute with the resource
the resource manager that can release the resource
whatever the block returns
Used with any object that contains a close()
method that
returns nothing, withCloseable()
executes a block of code
with the closeable object, ensuring that the object is closed no
matter what.
Used with any object that contains a close()
method that
returns nothing, withCloseable()
executes a block of code
with the closeable object, ensuring that the object is closed no
matter what. It allows you to replace code like this:
val closeableObject = ... try { doSomethingWith(closeableObject) } finally { closeableObject.close }
with:
withCloseable(closeableObject) { closeable =>
doSomethingWith(closeable)
}
the closeable object
the block of code, which will take the closeable object
and return some arbitrary type R
.
whatever the code block returns,if anything.
(Since version 1.5.1) Use grizzled.util.withResource
Miscellaneous utility functions and methods not otherwise categorized.