Skip to content

Commit

Permalink
Merge pull request #215 from AndrejMitrovic/ScopeDeprecation
Browse files Browse the repository at this point in the history
Add scope class to deprecation list.
  • Loading branch information
alexrp committed Dec 22, 2012
2 parents be9afba + 00ff8d6 commit 84583c0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions deprecate.dd
Expand Up @@ -40,6 +40,7 @@ $(SPEC_S Deprecated Features,
$(TROW $(DEPLINK .sort and .reverse properties for arrays), future,  ,  ,  ,  )
$(TROW $(DEPLINK delete), future,  ,  ,  ,  )
$(TROW $(DEPLINK overriding without override), future,  ,  ,  ,  )
$(TROW $(DEPLINK scope for allocating classes on the stack), future,  ,  ,  ,  )
)

$(DL
Expand Down Expand Up @@ -622,6 +623,52 @@ class B : A
$(P Making the 'override' attribute mandatory makes it explicit, and can catch errors when a base class function is accidentally overridden.
)

<h3>$(DEPNAME scope for allocating classes on the stack)</h3>
The $(D scope) keyword can be used to allocate class objects on the stack:
---
class A
{
int x;
this(int x) { this.x = x; }
}

void main()
{
A obj;
{
scope A a = new A(1);
obj = a;
}
assert(obj.x == 1); // fails, 'a' has been destroyed
}
---
<h4>Corrective Action</h4>
$(P Use $(B scoped) from std.typecons
---
class A
{
this(int x) { }
}
void main()
{
A a = scoped!A(1);
}
---
)
<h4>Rationale</h4>
$(P $(D scope) was an unsafe feature. A reference to a scoped class could easily
be returned from a function without errors, which would make using such
an object undefined behavior due to the object being destroyed after
exiting the scope of the function it was allocated in. To discourage it
from general-use but still allow usage when needed a library solution was
implemented.
)

$(P
Note that $(D scope) for other usages (e.g. scoped variables) is unrelated
to this feature and will not be deprecated.
)



)
Expand Down

0 comments on commit 84583c0

Please sign in to comment.