Automatic Resource Management

Reinier Zwitserloot reinier at zwitserloot.com
Mon Apr 13 16:54:54 PDT 2009


If the notion that 1 interface, with the associated forced 'close()'  
method, is considered too great of a problem, then your proposal is  
certainly the only viable alternative IMO. You can even play with the  
syntax to open multiple resources (but personally I'm somewhat opposed  
to letting any kind of ARM block open multiple resources in one  
statement; what's the problem with nesting a few of them?).


The problem I have with your proposal is twofold. One is simple, one  
is fairly esoteric:

1. Disposable/close() fits almost all use-cases, and the foreach  
syntax has shown that not covering every single existent base is not  
going to be a problem; the java world will adapt and come around to  
it. Especially because the libraries that can't work with this scheme  
don't seem too important to the java community at this point (more on  
that later).

2. The esoteric one: For semantics' sake, I would consider the closing  
statement to be better located at the end of the guarded block - but  
how do we figure this out, syntax wise? You could use the 'finally'  
keyword, which makes some sense, but the finally keyword already has  
meanings which are not quite compatible with the meaning of ARM-style  
final code: exceptions in the main body have precedence in ARM blocks,  
but they lose out in the usual try/finally. This makes finally a bad  
keyword, and in fact gets in the way of the entire concept: It LOOKS  
like you're just using a slightly different version of try/finally  
syntax, but that's not really what's going on. That makes your  
proposal somewhat ambiguous without knowing the JLS specifics. It's  
not nearly as bad as some of the other proposals, so not a  
showstopper, but certainly a nit.


Having said that, there might be a way out of this, by integrating  
both proposals.

1. Remove the ability to put multiple resources in one statement;  
what's the problem with nesting trywith blocks again?

2. Use the ; to switch to the alternate form of resource management:  
try ( a ; b ) {} no longer requires that is an expression of type  
'Disposable' (or a variable declaration of a Disposable), and instead,  
b is run at the end analogous to your proposal.


Then, for all objects that know how to dispose of themselves, you just  
go try ( object ) { stuff }, but when they don't, you can work around  
it with try ( object ; closeIt ) { stuff }.

NOTE: I'm not actually in favour of this, because, at the risk of  
taking a contentious position, I don't rate the libraries that can't  
deal with Disposable very highly. SWT was absolutely excellent when it  
was released, and the java community owes it a great thank you for  
lighting the fire of competition under awt/swing's behind, but between  
new swing versions and javaFX, SWT's time has gone, eclipse needs to  
migrate over, and SWT needs to slowly but surely go away now. Locks  
are handled just as well, IMO, with the existing try/finally  
structure, primarily because lock.unlock() generally doesn't throw  
exceptions, which is a big reason for using ARM in the first place.

  --Reinier Zwitserloot



On Apr 14, 2009, at 01:34, Peter Levart wrote:

> Hello Reinier,
>
> What do you think of the idea of explicit resource cleanup (as  
> opposed to
> using interface) that I have described a few days ago (in
> http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001383.html) 
> .
>
> The way I see it is best described by the following parallelism:
>
> Cleaning up resources correctly with existing try-catch-finally  
> construct as
> opposed to using Joshua's ARM is like iterating over elements of the  
> array
> using while loop as opposed to using foreach.
>
> int i = 0;
> while (i < array.length)
> {
> 	Type a = array[i] ;
> 	...
> 	i++;
> }
>
> vs.
>
> foreach (Type a : array)
> {
> 	...
> }
>
>
> ... but using existing  try-catch-finally vs. Semi-Automatic- 
> Resource-Cleanup
> (described in my previous mail) is like using while vs. for
>
> int i = 0;
> while (i < array.length)
> {
> 	Type a = array[i] ;
> 	...
> 	i++;
> }
>
> vs.
>
> for (int i = 0; i < array.length; i++)
> {
> 	Type a = array[i];
> 	...
> }
>
>
> ...there's not much less typing, everything is still explicit. But  
> the idiom
> is "captured" in a predefined construct leaving less chance for an  
> average
> programmer to do it wrong.
>
> Using 'for' instead of 'foreach' leaves you more flexibility if you  
> need it.
> For example:
>
> for (int i = offset; i < array.length && i < offset + length; i+ 
> +) ... etc.
>
> The question is: Do we want to be more flexible (and type a little  
> more) when
> capturing the resource cleanup idiom?
>
> Regards, Peter
>




More information about the coin-dev mailing list