Skip to content

Commit 1818c77

Browse files
authored
[spec/struct] Improve struct destructor docs (#3638)
Mention dtor called before assignment. Add example. Add link to RAII. Add heading for union field destruction. Mention struct dtor in RAII glossary and link to scope class variables.
1 parent bb6e7c8 commit 1818c77

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

spec/glossary.dd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ void test()
213213
the $(LINK2 $(ROOT_DIR)spec/const3.html, language rules).)
214214

215215
$(DT $(LNAME2 raii, $(ACRONYM RAII, Resource Acquisition Is Initialization)))
216-
$(DD RAII refers to the technique of having the destructor
217-
of a class object called when the object goes out of scope.
216+
$(DD RAII refers to the technique of having the
217+
$(DDSUBLINK spec/struct, struct-destructor, destructor
218+
of a struct) or class object called when the object goes out of scope.
218219
The destructor then releases any resources acquired by
219220
that object.
220221
RAII is commonly used for resources that are in short supply
221222
or that must have a predictable point when they are released.
222-
RAII objects in D are created using the $(D scope) storage class.
223+
RAII can be used for class variables declared with the
224+
$(DDSUBLINK spec/attribute, scope-class-var, $(D scope) storage class).
223225
)
224226

225227
$(DT $(LNAME2 rvalue, rvalue))

spec/struct.dd

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,13 +1770,47 @@ void main()
17701770

17711771
$(H2 $(LEGACY_LNAME2 StructDestructor, struct-destructor, Struct Destructors))
17721772

1773-
$(P Destructors are called when an object goes out of scope.
1773+
$(P Destructors are called when an object goes out of scope, or
1774+
$(RELATIVE_LINK2 assign-overload, before an assignment).
17741775
Their purpose is to free up resources owned by the struct
17751776
object.
17761777
)
17771778

1779+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1780+
---
1781+
struct S
1782+
{
1783+
int i;
1784+
1785+
~this()
1786+
{
1787+
import std.stdio;
1788+
writeln("S(", i, ") is being destructed");
1789+
}
1790+
}
1791+
1792+
void main()
1793+
{
1794+
auto s1 = S(1);
1795+
{
1796+
auto s2 = S(2);
1797+
// s2 destructor called
1798+
}
1799+
S(3); // s3 destructor called
1800+
// s1 destructor called
1801+
}
1802+
---
1803+
)
1804+
$(P The destructor can also be called early using $(REF1 destroy, object)
1805+
or by assigning the struct's `init` property.)
1806+
1807+
$(P Struct destructors are used for $(DDSUBLINK spec/glossary, raii, RAII).)
1808+
1809+
1810+
$(H2 $(LNAME2 union-field-destruction, Union Field Destruction))
1811+
17781812
$(P Unions may have fields that have destructors. However, a union itself never has
1779-
a destructor. When a union goes out of scope, destructors for its fields are not called.
1813+
a destructor. When a union goes out of scope, destructors for its fields *are not called*.
17801814
If those calls are desired, they must be inserted explicitly by the programmer:)
17811815

17821816
$(SPEC_RUNNABLE_EXAMPLE_COMPILE

0 commit comments

Comments
 (0)