I am trying to write a simple helper method that gets something that can be closed, and some function that gets the first and ensures that the closure closes after the function is executed.
For example, I want to use it as follows:
closing(new FileOutputStream("/asda"))(_.write("asas"))
My impl is
object Helpers {
def closing[T <: { def close }](closeable: T)(action: T => Unit): Unit =
try action apply closeable finally closeable close
}
But when trying to compile this simple test:
object Test {
import Helpers._
closing(new FileOutputStream("/asda"))(_.write("asas"))
}
The compiler complains:
arguments of input type [java.io.FileOutputStream] do not match the closure type of the parameter border method [T <: AnyRef {def close: Unit}]
Any ideas why?
source
share