Here is code in Scala:
def write() = {
try {
val out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)))
out.println("123")
out.close
} catch {
case e: IOException => {}
}
//finally {
//out.close // ops, it's not visible in this context
//}
}
It would be better to have "out.close
" in finally
block, isn't it? But I don't want to use var.
My question is, how do I achieve that?
The loan pattern is more usual for this use case, but since anything goes on Stack Overflow, you can construct the expression you're looking for with
Try
.Try
deserves more exposure as a handy tool.Try to open the file --
then do something with it, packaging the result in a tuple with the i/o source -- note that when you do a value definition in a for-comprehension, this is what it does --
then close the source and yield the result of the computation --
Uncommented:
Create the test file with some classic humour text, then try again:
You can sugarcoat it as a for-comprehension, though observe the extra flatten, since you get a map instead of flatMap from the yield:
What if we want to fail if the close fails?
I don't want to type in all that stuff into the REPL again!
The
transform
says that if the computation succeeded, convert that success to failure if the result of the close,ok
, is a failure; and on a failed computation, keep that failure, though some people prefer to add up their failures.Don't you know try/catch is so 1990s. </droll> (droll does not mean troll.)
Or just:
Because there's no way
is
can be null.In your question code, just move
val out
outside try block. That way it will be identical to what Java AutoCloseable does, except for null case. But in Scala you don't have to deal with nullables.This is what I use to manage closable resources passed to function returning and not retuning futures
I use
IOUtils
fromcommons-io
to simplify the call to actually close the resource. A simpletry { closable.close() } catch { case _ => /* blah */ }
would doExample usage:
withClosable(new FileInpustream("f")) { stream => /* read the stream */ }
A variable defined in a block is local to that block. So if you insist on using try/finally manually you will have to move the val out of the block.
However, what you are trying to achieve is to create a resource, use it in a block, and call a close method on it when leaving the block, no matter whether you leave the block normally or abnormally via an exception. This is an extremely common problem, so there is already a library for it, called Scala ARM. ARM stands for automatic resource management.
Here is the basic usage:
There was some talk of moving this construct to the scala standard library, so in the future you probably won't even need an external dependency.
I would recommend using a library for something like this. It is just one more line in your build.sbt. But for educational purposes, here is how you would roll your own:
And here is how to use it
You will still get the exception, but close will be called. Of course if close throws an exception as well you this will hide the original exception. Little details like this are probably handled better in scala ARM.