Proposal: Automatic Resource Management

Joshua Bloch jjb at google.com
Mon Mar 9 16:27:29 PDT 2009


Vilya,

Thanks for the feedback.  I am glad that you like the proposal.

On Mon, Mar 9, 2009 at 12:52 PM, Vilya Harvey <vilya.harvey at gmail.com>wrote:

>
>
> The only thing in it I wasn't too sure about was having variable
> declarations inside the parentheses. It works well in the examples given,
> but I can think of a few cases where it gets a bit ugly:
>
>   - using an anonymous class as an adapter to implement AutoCloseable when
>   an existing class doesn't implement it;
>   - using with a resource managing class which requires you to call a void
>   connect() method or similar to actually get the resource, instead of
>   grabbing it in the constructor.
>
> Both of these would be slightly better if expressions (i.e. references to
> existing variables) were allowed inside the parentheses. You can work
> around
> this under the existing proposal:
>
> AutoCloseable ac = ... ;
> try (AutoCloseable acRef = ac) { ... }
>
> but that seems (to me) to obscure the fact that ac will be closed
> afterwards
> even more than if ac was listed on it's own inside the parentheses.
>

The tradeoff is that allowing an expression in  place of a declaration  ups
the odds of someone accidentally accessing  a resource that's already
closed.  I could go either way on this.

         Josh



More information about the coin-dev mailing list