Skip to content

Commit ac82841

Browse files
authored
[spec] Improve class destructor docs (#3673)
* [spec] Improve class destructor docs List when dtor called including `destroy`. Minor tweaks. Change 'auto object' to 'scope class instance' * Use IMPLEMENTATION_DEFINED and PANEL macro Whether the GC calls the destructor is implementation defined. Use 'Important' heading for the order of destructor calls. Use PANEL macro to split paragraph in two whilst grouping. * Fix wording about destroy as it applies to scope instances too * Make example runnable * Use list for destructor characteristics
1 parent c08a41f commit ac82841

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

spec/class.dd

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -718,57 +718,74 @@ $(GNAME Destructor):
718718
$(D ~ this ( )) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
719719
)
720720

721-
$(P The destructor function is called when the object
722-
is deleted by the garbage collector, or when a
723-
$(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance) goes out of scope.
724-
The syntax is:)
721+
$(P The destructor function is called when:)
725722

723+
* A live object is deleted by the garbage collector
724+
* A live $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance) goes out of scope
725+
* $(REF1 destroy, object) is called on the object
726+
727+
$(P Example:)
728+
729+
$(SPEC_RUNNABLE_EXAMPLE_RUN
726730
------
731+
import std.stdio;
732+
727733
class Foo
728734
{
729735
~this() // destructor for Foo
730736
{
737+
writeln("dtor");
731738
}
732739
}
733-
------
734740

735-
$(P There can be only one destructor declared per class, although
736-
other destructors $(DDSUBLINK spec/template-mixin, destructors, can be mixed in).
737-
A destructor does not have any parameters,
738-
and has no attributes. It is always virtual.
741+
void main()
742+
{
743+
auto foo = new Foo;
744+
destroy(foo);
745+
writeln("end");
746+
}
747+
------
739748
)
740749

741-
$(P The destructor is expected to release any resources held by the
750+
* Only one destructor can be declared per class, although
751+
other destructors $(DDSUBLINK spec/template-mixin, destructors, can be mixed in).
752+
* A destructor does not have any parameters or attributes.
753+
* A destructor is always virtual.
754+
755+
$(P The destructor is expected to release any non-GC resources held by the
742756
object.
743757
)
744758

745-
$(P The program can explicitly inform the garbage collector that an
746-
object is no longer referred to with $(REF1 destroy, object), and
747-
then the garbage collector calls the destructor immediately. The
748-
destructor is guaranteed to never be called twice.
759+
$(P The program can explicitly call the destructor of a live
760+
object immediately with $(REF1 destroy, object).
761+
The runtime marks the object so the destructor is never called twice.
749762
)
750763

751-
$(P The destructor for the super class automatically gets called when
752-
the destructor ends. There is no way to call the super destructor
764+
$(P The destructor for the $(RELATIVE_LINK2 super_class, super class) automatically gets called when
765+
the destructor ends. There is no way to call the super class destructor
753766
explicitly.
754767
)
755768

756-
$(P The garbage collector is not guaranteed to run the destructor
757-
for all unreferenced objects. Furthermore, the order in which the
769+
$(IMPLEMENTATION_DEFINED The garbage collector is not guaranteed to run the destructor
770+
for all unreferenced objects.)
771+
772+
$(PANEL
773+
$(DIVC spec-boxes, $(B Important:) The order in which the
758774
garbage collector calls destructors for unreferenced objects
759775
is not specified.
760776
This means that
761777
when the garbage collector calls a destructor for an object of a class
762778
that has
763779
members which are references to garbage collected objects, those
764780
references may no longer be valid. This means that destructors
765-
cannot reference sub objects.
766-
This rule does not apply to auto objects or objects destructed
767-
with $(REF1 destroy, object), as the destructor is not being run
768-
by the garbage collector, meaning all references are valid.
781+
cannot reference sub objects.)
782+
783+
$(NOTE This rule does not apply to a `scope` class instance or an object destructed
784+
with `destroy`, as the destructor is not being run
785+
during a garbage collection cycle, meaning all references are valid.)
769786
)
770787

771-
$(P Objects referenced from the data segment never get collected
788+
$(P Objects referenced from the static data segment never get collected
772789
by the GC.
773790
)
774791

0 commit comments

Comments
 (0)