Following these examples and especially this code:
object Control {
def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
try {
f(resource)
} finally {
resource.close()
}
}
...
using(io.Source.fromFile("example.txt")) { source => { .....
I wanted to extend the using
method so instead of a type which implements close
it receives a string (filename), a function to open a source, and the processing function. In this way, I would avoid the exception which would be thrown in the above example in case the given file does not exist.
So I ended up with this code:
object Control
{
def using[A <: { def close(): Unit }, B](opener: String => A)(name:String)(func: A => B): Unit =
{
var resource:A
// ^ Error: 'Block cannot contain declarations'
try
{
resource = opener(name)
func(resource)
}
catch
{
case e: (_) => println(s"Failed to open resource '${name}' (${e})")
}
finally
{
println("Closing file ...")
resource.close()
}
}
}
So I am defining a method, which takes as first parameter an opener
-function, which receives a string, and returns an object which implements close
, a string (for the opener function), and a processing function.
However it won't let me declare the resource
variable outside of the try-catch
block (so I can reach it in the finally
block). It will work if I just put it into the try
block like var resource:A = opener(name)
, however then I cannot reach resource
in the finally
block.
How can I solve it? I have to say that I am still a beginner in Scala, so I am a bit lost here.