September 01, 2021

On Wednesday, 1 September 2021 at 18:20:59 UTC, Paul Backus wrote:

>

On Monday, 30 August 2021 at 16:12:57 UTC, Adam D Ruppe wrote:

>

On Monday, 30 August 2021 at 16:03:29 UTC, Guillaume Piolat wrote:

>

Hyped by ProtoObject, this is our hope for a nothrow @nogc .destroy eventually!

This fails today only because of the rt_finalize hook working through void*. If you cut that out...

class Foo {
        ~this() @nogc nothrow {}
}

void main() @nogc nothrow {
        scope auto foo = new Foo();
        foo.__xdtor();
}

this works today.

I thought the problem with this was that destructors aren't virtual, so if you write something like this:
[...]
...then you end up calling Foo's destructor, but not Bar's. That's why rt_finalize uses TypeInfo to look up the destructor for the object's runtime type.

we can use custom destructors:

class Foo {
    ~this() nothrow @nogc  {}
    void destroy() nothrow @nogc
    {
        __xdtor();
    }
}

class Bar : Foo {
    override void destroy()
    {
        super.destroy();
    }
}

void main() {
    Foo foo = new Bar();
    foo.destroy();
}

I dont know why destructors are not virtual. This makes sense for constructors as the correct instance size is required but not for destructors.

September 01, 2021

On Wednesday, 1 September 2021 at 22:23:59 UTC, user1234 wrote:

>

I dont know why destructors are not virtual.

https://dlang.org/spec/class.html#destructors

"There can be only one destructor per class, the destructor does not have any parameters, and has no attributes. It is always virtual. "

I guess it is virtually virtual due to the rt_finalize implementation papering it over.

But you can write a destroy function to do this too by looking up the base class xdtor as well (I'm pretty sure anyway).

My point is really just that protoobject is almost certain to have exactly the same destructor situation as object....

1 2
Next ›   Last »