JDK 9 RFR of JDK-8068948: Update java.base module to use new try-with-resources statement

Ivan Gerasimov ivan.gerasimov at oracle.com
Fri Jan 16 17:46:37 UTC 2015


>> Might it also make sense to allow anonymous variables in the 
>> try-with-resource statement?
>> So that something like `try (() -> System.out.println("closed")) {}` 
>> would work...
>
> I don't think that is necessary. In JDK 7, we started out allowing a 
> general AutoCloseable expression in a try-with-resources statement and 
> that proved problematic. Supporting a final / effectively final 
> variable seems to be the right balance. The rationale is written up in 
> the JSR 334 materials.
>

Yes, I agree. And it can easily be moved to the finally block, of course.
However, this:

         ExecutorService executor = ...;
         try (executor::shutdown) {
         }

still looks a bit more attractive to me, comparing to `try (Closeable c 
= executor::shutdown) {`
Maybe one day ... :-)

Sincerely yours,
Ivan




More information about the core-libs-dev mailing list