On Mon, 04 Jan 2010 05:52:19 -0500, bearophile <[email protected]>
wrote:
If I compile the following code with DMD with and without the scope
annotation I can see that both versions compile and the version with
scope deletes the object. Is the compiler acting correctly here? I'd
like the compiler to refuse to compile this code when the scope
attribute is present (this is a reduced example from a bug I've just
removed from a program of mine):
class Foo {}
class Bar {
Foo x;
void spam() {
scope Foo temp = new Foo();
this.x = temp;
}
}
void main() {}
Scope is not a type constructor, so once the compiler passes the line
scope Foo temp = new Foo();
the compiler sees temp as type Foo, not scope Foo. So it is ignorant on
the next line to know to stop you from doing something foolish.
When using scope, it is on you to ensure that it doesn't escape.
I think the compiler cheats a little bit on delegates, but it has severe
limitations that I think make using delegates without accidentally
allocating closures difficult.
-Steve