Automatic Resource Management, V.2

Neal Gafter neal at
Mon Apr 20 13:07:42 PDT 2009

On Mon, Apr 20, 2009 at 12:59 PM, Paul Martin <paul.martin at> wrote:

> Also, one question from:
> > java.util.Iterator&AutoDisposable:
> > "We can learn from C# here, and avoid the same mistake that C# avoided.
>  The simple solution is to make the for-each loop dispose the iterator if it
> implements AutoDisposable, as C# does. The point, in part, is to make the
> proposed construct not just useful in code that already exists (criterion
> [2]), but useful in code yet to be written."
> How can the compiler do this (determine whether an Iterable is also
> AutoDisposable)?  An Iterable interface passed to the for-each loop
> might not be defined as AutoDisposable, but its runtime implementation
> might be.  I don't see how this can be statically determined by the
> compiler, and I wouldn't think that we would want the type of each
> Iterable to be checked at runtime to determine whether it also
> implements AutoDisposable (in case some objects are AutoDisposable and
> other objects are not).

It's not the type of the Iterable that I suggest have special treatment, it
is the Iterator.  Since that iterator object is created by
compiler-generated code and never visible to the user program, it makes
quite a lot of sense for the generated code to arrange its disposal when

More information about the coin-dev mailing list