views:

142

answers:

3

I'd like to have a FileWriter opened during the whole time a class instance exists. So I need to close it in a destructor. But how to specify a destructor in Scala?

+7  A: 

Scala doesn't have destructors. It has finalizers, like Java, but they're not the same thing at all. There is also an interesting blog series in emulating C#'s using keyword in Scala here:

Marcelo Cantos
See also the [`Closeable`](http://download.oracle.com/javase/7/docs/api/java/io/Closeable.html) interface (specifically for IO), and the up-coming (Java 7) more general [`AutoCloseable`](http://download.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html).
Matthew Flaschen
@Matthew: Don't you think that's more of an alternative answer than a comment on my answer?
Marcelo Cantos
@matthew-flaschen do you mean a FileWriter object will auto close when it's GCd?
Ivan
@marcelo-cantos Using doesn't look good because it is to be used inside a method, opening and closing a file every time I call it. I think this will ruin overall performance as it is going to be called very very frequently. I want one FileWriter object to be opened all the time.
Ivan
@Ivan: Can you instantiate the FileWriter in the outer loop and pass it in (either as a parameter on each call or assigned to a field)?
Marcelo Cantos
@marcelo-cantos I can hardly imagine how to implement it this way and not make my code ugly. Actually now I have "write" method in the class and store a FileWriter in the class's private field. "write" method initializes the FileWriter if it isn't initialized, writes data and leaves it open, so I need to close it somewhere.
Ivan
@Ivan: Then you'll just have to go for the traditional make-sure-you-close-the-door-on-your-way-out approach used since the earliest days of C.
Marcelo Cantos
@marcelo-cantos I've got an idea! I myself am going to implement Closeable, so that upstanding code will close my class instance it has created, and I can close the file in my class's close method.
Ivan
There's an arm library, as Kris have indicated: http://github.com/jsuereth/scala-arm
Daniel
+6  A: 

You might be interested to check out Josh Suereth's scala-arm project, which provides both monadic and delimited-continuation based resource management for just this source of use: http://github.com/jsuereth/scala-arm

If you really think that you need a destructor (i.e. because you think you need to create the object and then hand it off and never see it again) I'd recommend reconsidering your application architecture instead... there is simply no way to make this work reliably on the JVM.

Kris Nuttycombe
+1  A: 

As I noted above, Java has an existing Closeable interface specifically for IO, which you could adopt. This doesn't provide any sugar, but it will help people use your class correctly.

In Java 7, Closeable will be a subinterface of AutoCloseable. AutoCloseable is a more general interface for any resource that needs to be closed after use while potentially throwing an exception. it is part of the planned Automatic Resource Management support in Java 7. Less relevant to your question (since you're using Scala), there is also supposed to be new Java syntax (an extension of existing try blocks) for this scenario.

Matthew Flaschen