On 9/4/11 2:58 PM, Jacob Carlborg wrote:
On 2011-09-04 14:59, Andrei Alexandrescu wrote:
On 9/4/11 7:11 AM, Jacob Carlborg wrote:
Tango has added a new method to Object, "dispose". The method is called
by the runtime when a scoped class exits a scope:

void foo ()
{
scope f = new File;
}

When "foo" exits File.dispose will be called and it can close any file
handles. I think it's quite clever.

What happens if f is aliased beyond the existence of foo()?

Andrei

I'm not sure if this is what you mean but:

File file;

void foo ()
{
scope f = new File;
file = f;
}

void main ()
{
foo;
// file is disposed here
}

In the above example "dispose" will be called when "foo" exits. After
the call to "foo" in the main function "file" will refer to an object
that is disposed, i.e. an object where the "dispose" method has been
called.

I don't know how bad this is or if it is bad at all.

Well it's not bad but a bit underwhelming. Clearly it's better than the unsafe behavior of scope, but it's nothing to write home about. The grand save it makes is replacing "scope(exit) f.dispose();" with "scope" in front of the declaration. That does systematically save some typing, but it's a feature with only local, non-modular effect, and limited abstraction power.


Andrei

Reply via email to