Why is `scope` planned for deprecation?
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Mon Nov 10 20:44:44 PST 2014
On 11/10/14 6:33 PM, Mike wrote:
> First of all, what exactly is planned for deprecated? It [1] says "Note
> that scope for other usages (e.g. scoped variables) is unrelated to this
> feature and will not be deprecated.", but the example...
>
> void main()
> {
> A obj;
> {
> scope A a = new A(1);
> obj = a;
> }
> assert(obj.x == 1); // fails, 'a' has been destroyed
> }
>
> .... looks a lot like a scoped variable to me, so it's not clear to me
> what exactly is planned for deprecation. Please clarify.
>
>
> Ok, with that out of the way, I get why it is unsafe, but isn't it only
> unsafe because it has not yet been implemented? Isn't it possible to
> implement escape analysis and make it a safe and useful feature?
>
> This question was asked before, but never received an answer.[2]
>
> Mike
>
> [1]
> http://dlang.org/deprecate.html#scope%20for%20allocating%20classes%20on%20the%20stack
>
> [2] http://forum.dlang.org/post/k549l4$1s24$1@digitalmars.com
Well, that's a funny thing. I looked it up, apparently using scope to
designate "scope variables" is a thing:
http://dlang.org/attribute.html#scope
" For local declarations, scope implements the RAII (Resource
Acquisition Is Initialization) protocol. This means that the destructor
for an object is automatically called when the reference to it goes out
of scope. The destructor is called even if the scope is exited via a
thrown exception, thus scope is used to guarantee cleanup. "
Anyone used to using structs for RAII would think WAT? But a long time
ago, structs did not have dtors. So I think at that time, scope simply
applied only to classes. Note how it specifically says "objects"
What I think it means is, scope declarations for allocating classes will
be destroyed when leaving scope, but will not be allocated on the stack.
I don't know why this is less dangerous. Perhaps it's destroyed but not
deallocated?
But the deprecation says "(e.g. scoped variables) is unrelated to this
feature." Seems pretty related.
My real guess is that the deprecation message is wrong. We have
scope(exit), I don't see why we would need scope variables as well.
-Steve
More information about the Digitalmars-d
mailing list