What is the purpose of -preview=dtorfields?

Simen Kjærås simen.kjaras at gmail.com
Mon Aug 31 20:49:21 UTC 2020

On Monday, 31 August 2020 at 20:22:02 UTC, Per Nordlöw wrote:
> I'd appreciate if somebody elaborated a bit on the purpose of 
> the compiler flag
>     -preview=dtorfields       destruct fields of partially 
> constructed objects

With this code:

struct S1 {
     this(int) {}
     ~this() {
         import std.stdio;

struct S2 {
     S1 s1;
     S1 s2;
     this(int i) {
         s1 = S1(0);
         throw new Exception("");
         s2 = S1(0);

void main() {
     S2 a = S2(2);

The current behavior is to not destruct the s1 and s2 fields, but 
-preview=dtorfields ensures they will be destructed. I kinda 
expected only s1 to be destructed above, but s2 follows it down 
the drain.


More information about the Digitalmars-d mailing list