@@ -1770,8 +1770,8 @@ void main()
1770
1770
1771
1771
$(H2 $(LEGACY_LNAME2 StructDestructor, struct-destructor, Struct Destructors))
1772
1772
1773
- $(P Destructors are called when an object goes out of scope, or
1774
- $(RELATIVE_LINK2 assign-overload, before an assignment).
1773
+ $(P Destructors are called implicitly when an object goes out of scope, or
1774
+ $(RELATIVE_LINK2 assign-overload, before an assignment) (by default) .
1775
1775
Their purpose is to free up resources owned by the struct
1776
1776
object.
1777
1777
)
@@ -1801,8 +1801,43 @@ void main()
1801
1801
}
1802
1802
---
1803
1803
)
1804
- $(P The destructor can also be called early using $(REF1 destroy, object)
1805
- or by assigning the struct's `init` property.)
1804
+ $(P If the struct has a field of another struct type which itself has a destructor,
1805
+ that destructor will be called at the end of the parent destructor. If there is no
1806
+ parent destructor, the compiler will generate one. Similarly, a
1807
+ static array of a struct type with a destructor will have the destructor
1808
+ called for each element when the array goes out of scope.)
1809
+
1810
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1811
+ ---
1812
+ struct S
1813
+ {
1814
+ char c;
1815
+
1816
+ ~this()
1817
+ {
1818
+ import std.stdio;
1819
+ writeln("S(", c, ") is being destructed");
1820
+ }
1821
+ }
1822
+
1823
+ struct Q
1824
+ {
1825
+ S a;
1826
+ S b;
1827
+ }
1828
+
1829
+ void main()
1830
+ {
1831
+ Q q = Q(S('a'), S('b'));
1832
+ S[2] arr = [S('0'), S('1')];
1833
+ // destructor called for arr[1], arr[0], q.b, q.a
1834
+ }
1835
+ ---
1836
+ )
1837
+
1838
+ $(P A destructor for a struct instance can also be called early using
1839
+ $(REF1 destroy, object). Note that the destructor will still
1840
+ be called again when the instance goes out of scope.)
1806
1841
1807
1842
$(P Struct destructors are used for $(DDSUBLINK spec/glossary, raii, RAII).)
1808
1843
@@ -1813,7 +1848,7 @@ $(H2 $(LNAME2 union-field-destruction, Union Field Destruction))
1813
1848
a destructor. When a union goes out of scope, destructors for its fields *are not called*.
1814
1849
If those calls are desired, they must be inserted explicitly by the programmer:)
1815
1850
1816
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1851
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1817
1852
---
1818
1853
struct S
1819
1854
{
0 commit comments