Proposal: Automatic Resource Management (Xavi Mir?)
David Walend
david at walend.net
Thu Sep 3 14:52:52 PDT 2009
On Sep 3, 2009, at 3:00 PM, coin-dev-request at openjdk.java.net wrote:
>
> Date: Thu, 03 Sep 2009 07:16:26 +0200
> From: Xavi Mir? <xmirog at gmail.com>
> Subject: Re: Proposal: Automatic Resource Management
> To: "Sean R. Drucker" <sean.drucker at frameworkplus.com>
> Cc: coin-dev at openjdk.java.net
> Message-ID: <4A9F512A.8080900 at gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I see this iterator autoclean construction very powerful but as a
> potential user of it I would prefer a slightly different syntax. The
> current proposal would change the semantics of existing programs, as
> Josh has pointed, and although it is likely that it wouldn't break
> existing programs, I think a different syntax would make clearer for a
> programmer to see it would do something more than the current foreach
> construction. I mean I would prefer something like:
>
> (1)
>
> try for (final Row row: table) {
> ...
> }
>
> or
>
> (2)
>
> for (final Row row: table) try {
> ...
> }
>
> instead of
>
> (3)
>
> for (final Row row: table) {
> ...
> }
>
> For me it's easier to understand with (1) or (2) that it's going to
> _try_ to do something _for_ each of those elements; it will iterate
> and
> for each element it will autoclean. With (3) I can see the same
> construction we use today, "for each element do this" and depending on
> the version of Java it will only iterate or it will iterate and
> autoclean. I know that only with elements that implement a Disposable
> interface it would autoclean, but in order to actually know it I must
> check the interfaces (which can be in another source code file);
> with a
> different syntax I could know it instantly.
>
> In addition, if we use a different syntax the compiler can complain if
> the elements don't implement Disposable, so if the programmer
> forgets to
> make them implement it he or she can notice it. If the syntax is the
> same, forgetting to implement the interface would be silently ignored
> and the programmer would think the elements would be cleaned up and
> they
> would not.
>
> The (1) and (2) syntax are only two possibilities, maybe we can find a
> better one, but in my humble opinion it would be best to differentiate
> the different semantics with different syntaxes.
>
> Regards,
>
> Xavi
>
(2) already compiles and works fine already.
int[] ints = {0,1,2,3};
for(final int i: ints) try {
System.out.println(i);
}
catch(RuntimeException re) {
..
(1) doesn't. I think it's only because "try" requires {}s, which is
inconsistent with other kinds of control flow.
Maybe use something that doesn't look like it would work now:
try (final Row row: table) {
...
}
Dave
More information about the coin-dev
mailing list