Proposal: Automatic Resource Management

Mark Mahieu markmahieu at
Wed Mar 4 13:45:26 PST 2009


On 4 Mar 2009, at 19:09, Joshua Bloch wrote:
>  I think the solution must necessarily be a bit hackish.  Here's  
> one possibility, based loosely on the way for-each works.  A  
> resource can implement one of several interfaces.  One (currently  
> named Disposable) contains a close method.  Another (name tbd)  
> contains a dispose method.  A third might contain a destroy  
> method.  I can't imagine an existing type to which none of these  
> three interfaces can be retrofitted.  The SWT Resource type could  
> be retrofitted to implement the interface with the dispose method,  
> without even adding a new method. This isn't beautiful, but it  
> works.  I think it's probably a good idea.

Well, since we probably can't iron out every inconsistency with  
what's gone before, we could try to be consistently ironic:

interface Disposable {
	void close() throws Exception;

interface Destroyable {
	void dispose() throws Exception;

interface Kloseable {
	void destroy() throws Exception;


In all seriousness, what should the behaviour then be if a class  
implements two or more of these interfaces?  Should all of the  
'disposal' methods be invoked?

Putting interfaces aside for a moment, instead of an annotation my  
instinct would have been to reach for a modifier here, especially  
given that this proposal is for dedicated language support rather  
than a library based solution.  There's even one reserved word  
already that conveys the meaning pretty well:

class SomeResourceType {

	public finally void release() {
		// ...

That approach may well raise all sorts of questions as well - I can  
certainly think of a couple - but compared to using an annotation,  
what's wrong with it?


More information about the coin-dev mailing list