Skip to content

Commit aa232e2

Browse files
authored
Fix Issue 23946 - specifications state that "there can only be one destructor" (#3636)
...which can be confusing because of mixin templates
1 parent 1818c77 commit aa232e2

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

spec/class.dd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,9 @@ $(GNAME Destructor):
716716
}
717717
------
718718

719-
$(P There can be only one destructor per class, the destructor
720-
does not have any parameters,
719+
$(P There can be only one destructor declared per class, although
720+
other destructors $(DDSUBLINK spec/template-mixin, destructors, can be mixed in).
721+
A destructor does not have any parameters,
721722
and has no attributes. It is always virtual.
722723
)
723724

spec/template-mixin.dd

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ void main()
334334
}
335335
-----
336336

337-
$(H2 $(LNAME2 virtual_functions, Mixins and Virtual Functions))
337+
$(H2 $(LNAME2 aggregate_types, Aggregate Type Mixins))
338+
339+
$(H3 $(LNAME2 virtual_functions, Mixin Virtual Functions))
338340

339341
$(P Mixins can add virtual functions to a class:)
340342

@@ -368,6 +370,41 @@ void main()
368370
------
369371
)
370372

373+
$(H3 $(LNAME2 destructors, Mixin Destructors))
374+
375+
$(P An aggregate type can mixin additional destructors.
376+
Destructors are run in the opposite order to declaration order.)
377+
378+
$(SPEC_RUNNABLE_EXAMPLE_RUN
379+
---
380+
import std.stdio;
381+
382+
mixin template addNewDtor()
383+
{
384+
~this()
385+
{
386+
writeln("Mixin dtor");
387+
}
388+
}
389+
390+
struct S
391+
{
392+
~this()
393+
{
394+
writeln("Struct dtor");
395+
}
396+
397+
mixin addNewDtor;
398+
}
399+
400+
void main()
401+
{
402+
S s;
403+
// prints `Mixin dtor`
404+
// prints `Struct dtor`
405+
}
406+
---
407+
)
371408

372409
$(SPEC_SUBNAV_PREV_NEXT template, Templates, contracts, Contract Programming)
373410
)

0 commit comments

Comments
 (0)