www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - private final class destructors

reply bearophile <bearophileHUGS lycos.com> writes:
According to TDPL the whole object destructor chain is called up to the top of
the hyerarchy.
This D2 program compiles and runs with DMD 2.048 with no errors:


import std.c.stdio: puts;
class Base {
     private final ~this() { puts("Base.~this"); }
}
class Derived : Base {
    private final ~this() { puts("Derived.~this"); }
}
void main() {
    new Derived();
}


Output:

Derived.~this
Base.~this


Are the 'private final' attributes used here correct?
See also:  http://d.puremagic.com/issues/show_bug.cgi?id=3934

Bye,
bearophile
Aug 19 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
bearophile <bearophileHUGS lycos.com> wrote:

 According to TDPL the whole object destructor chain is called up to the  
 top of the hyerarchy.
 This D2 program compiles and runs with DMD 2.048 with no errors:


 import std.c.stdio: puts;
 class Base {
      private final ~this() { puts("Base.~this"); }
 }
 class Derived : Base {
     private final ~this() { puts("Derived.~this"); }
 }
 void main() {
     new Derived();
 }


 Output:

 Derived.~this
 Base.~this


 Are the 'private final' attributes used here correct?
 See also:  http://d.puremagic.com/issues/show_bug.cgi?id=3934

A destructor is always final, and private destructors make no sense, so I would say the attributes should not be there. -- Simen
Aug 19 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Simen kjaeraas:
 A destructor is always final, and private destructors make no sense,
 so I would say the attributes should not be there.

Thank you for your answer, I have added the test case to bug 3934 Bye, bearophile
Aug 19 2010
prev sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Thursday, August 19, 2010 08:23:42 Simen kjaeraas wrote:
 A destructor is always final, and private destructors make no sense,
 so I would say the attributes should not be there.

Actually, I could see private destructors making at least some sense. Obviously, the runtime needs to be able to call them, but I could see someone wanting to make it so that a destructor could not be explicitly called by the programmer. I'm not sure that that's ultimately all that useful, but I could see private destructors working that way. - Jonathan M Davis
Aug 19 2010