This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Equivalent to Java 7's Automatic Resource Management (try-with-resources)

6 replies
Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
In my personal Scala code playground, I've been tinkering with something that would auto-close resources at the end of a block for a while, and I've cleaned up my implementation to be self-encapsulated:
https://github.com/tvierling/duh-util/blob/master/src/main/scala/org/duh/util/AutoClose.scala

This introduces an implicit wrapper (AutoClose.RichCloseable) that provides two forms of an added method "autoClose": one uses a by-name block; the other will provide the wrappered resource as a single argument. This wrapper works automatically with any class exposing a zero-arg close() method, which means that it works under all supported Java versions, not just 7+.
This implements the same overall semantics as Java 7's try-with-resources construct. It only handles one resource per nesting level, unlike the more complicated Java 7 syntax, but usage is pretty straightforward. Here's an example of both syntax forms; you'll note that the "out =>" version is the closest simulation of the Java 7 feature, as the value is not accessible outside the block:
def doStuff(in: InputStream) {  in.autoClose {    new FileOutputStream("foo").autoClose { out =>      out.write(/* ... */)    }
    // out is now closed  }  // in is now closed}
There's added logic in this implementation to provide built-in support for the Java 7+ suppressed-Throwable feature if the code is running under Java 7 or later, and a fallback mechanism for earlier versions (a pluggable InheritableThreadLocal of a handler function, which may perform special duties with to-be-suppressed Throwables under Java 6 or earlier).
As a speed hack, it uses a match expression on the wrapped object when finally calling close(), matching against several commonly used JDK classes with close() methods before deferring to the slower structural type logic. The truly important part of that match expression (IMO) is the match against java.io.Closeable; the others are a bit less important.
Other code in my playground reserves copyright just to keep from tripping up people with my probably-broken code (see README.md at top of the Github project), but I have released this specific source file to the public domain. If EPFL would like to include a version of this implementation in the standard Scala library, please feel free. Attribution in the derived source file would be nice, but is not required.
Thoughts and suggestions are always welcome.
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w

Note that this isn't exactly new territory. People have been doing ARM
in Scala way before Java even considered it.

See http://stackoverflow.com/questions/2207425/what-automatic-resource-manag...
for some history, and
https://github.com/jsuereth/scala-arm/wiki/basic-usage for the most
advanced library available (afaik).

On Fri, Aug 26, 2011 at 19:18, Todd Vierling wrote:
> In my personal Scala code playground, I've been tinkering with something
> that would auto-close resources at the end of a block for a while, and I've
> cleaned up my implementation to be self-encapsulated:
> https://github.com/tvierling/duh-util/blob/master/src/main/scala/org/duh...
>
> This introduces an implicit wrapper (AutoClose.RichCloseable) that provides
> two forms of an added method "autoClose": one uses a by-name block; the
> other will provide the wrappered resource as a single argument. This wrapper
> works automatically with any class exposing a zero-arg close() method, which
> means that it works under all supported Java versions, not just 7+.
> This implements the same overall semantics as Java 7's try-with-resources
> construct. It only handles one resource per nesting level, unlike the more
> complicated Java 7 syntax, but usage is pretty straightforward. Here's an
> example of both syntax forms; you'll note that the "out =>" version is the
> closest simulation of the Java 7 feature, as the value is not accessible
> outside the block:
> def doStuff(in: InputStream) {
>   in.autoClose {
>     new FileOutputStream("foo").autoClose { out =>
>       out.write(/* ... */)
>     }
>     // out is now closed
>   }
>   // in is now closed
> }
> There's added logic in this implementation to provide built-in support for
> the Java 7+ suppressed-Throwable feature if the code is running under Java 7
> or later, and a fallback mechanism for earlier versions (a pluggable
> InheritableThreadLocal of a handler function, which may perform special
> duties with to-be-suppressed Throwables under Java 6 or earlier).
> As a speed hack, it uses a match expression on the wrapped object when
> finally calling close(), matching against several commonly used JDK classes
> with close() methods before deferring to the slower structural type logic.
> The truly important part of that match expression (IMO) is the match against
> java.io.Closeable; the others are a bit less important.
> Other code in my playground reserves copyright just to keep from tripping up
> people with my probably-broken code (see README.md at top of the Github
> project), but I have released this specific source file to the public
> domain. If EPFL would like to include a version of this implementation in
> the standard Scala library, please feel free. Attribution in the derived
> source file would be nice, but is not required.
> Thoughts and suggestions are always welcome.
>

Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w
On Friday, August 26, 2011 6:38:40 PM UTC-4, Daniel Sobral wrote:
Note that this isn't exactly new territory. People have been doing ARM
in Scala way before Java even considered it.

Odd. When I originally went looking, I couldn't find such an implementation for some reason — but then, I'd been sitting on this for well over a year and letting it rot. Thanks for the pointers!

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w
If you have a look at the ARM library, let me know what you think.  I'm trying to go 1.0 shortly, so any suggestions before the release are welcome!

On Fri, Aug 26, 2011 at 7:00 PM, Todd Vierling <tv [at] duh [dot] org> wrote:
On Friday, August 26, 2011 6:38:40 PM UTC-4, Daniel Sobral wrote:
Note that this isn't exactly new territory. People have been doing ARM
in Scala way before Java even considered it.

Odd. When I originally went looking, I couldn't find such an implementation for some reason — but then, I'd been sitting on this for well over a year and letting it rot. Thanks for the pointers!


Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w

On Fri, Aug 26, 2011 at 8:48 PM, Josh Suereth wrote:
> If you have a look at the ARM library, let me know what you think.  I'm
> trying to go 1.0 shortly, so any suggestions before the release are welcome!

I'm taking a look at it this week. Cursory glance, it looks like some
hooking into the Java 7 suppressed exceptions mechanism could be added
(it can be made runtime-conditional with very low overhead, which I
did for my tiny implementation).

However, I don't yet know how your library handles multiple exceptions
thrown by cleanup routines, so I'll have to grok that first.

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w
I can add the low-overhead exception supressing mechanism.  If you want to contribute anything to the project, feel free to do so!

On Mon, Aug 29, 2011 at 8:46 PM, Todd Vierling <tv [at] duh [dot] org> wrote:
On Fri, Aug 26, 2011 at 8:48 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
> If you have a look at the ARM library, let me know what you think.  I'm
> trying to go 1.0 shortly, so any suggestions before the release are welcome!

I'm taking a look at it this week. Cursory glance, it looks like some
hooking into the Java 7 suppressed exceptions mechanism could be added
(it can be made runtime-conditional with very low overhead, which I
did for my tiny implementation).

However, I don't yet know how your library handles multiple exceptions
thrown by cleanup routines, so I'll have to grok that first.

--
Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Equivalent to Java 7's Automatic Resource Management (try-w

On Mon, Aug 29, 2011 at 8:56 PM, Josh Suereth wrote:
> I can add the low-overhead exception supressing mechanism.  If you want to
> contribute anything to the project, feel free to do so!

Github-forked and will get back to you soon.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland