Why will the delete keyword be removed?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jul 14 14:51:27 PDT 2010


On 07/14/2010 04:47 PM, Michel Fortin wrote:
> On 2010-07-14 15:43:00 -0400, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> said:
>
>> You mean class objects, right? I agree. I think it's okay to fill the
>> object with its stateless .init members, which would assuage the issue.
>
> That risks breaking the object's invariants. Next thing you know you'll
> get an assertion error in the object's invariants and will look inside
> your object wondering how its private state could ever have gone like
> that. What really happened is that somewhere far far away clear() will
> have been called, putting your object in an invalid state. Try to debug
> that!

I think you're not properly balancing the advantages and the 
disadvantages of the system.

> In my opinion, if you need to clear an object's state, the object itself
> should be designed with that in mind (probably offering a clear()
> method). The object should be the one to decide if it can become zombie
> or not, not the user.

That disables any guarantee that could be reliably made.

> clear() is memory safe (unlike delete), but beyond that it doesn't look
> much less dangerous to use to me. I'm also doubtful it won't violate
> immutability in the presence of immutable members. Take a look at this
> problem for instance:
>
> class A {
> immutable int i;
>
> this(int i) {
> this.i = i;
> }
> }
>
> A a = new A(10);
> immutable int* i = &a.i;
> assert(*i == 10);
> clear(a);
> assert(*i == 10); // i is immutable, isn't it?
>
> Be sure to try it with a struct too.

Good point.


Andrei


More information about the Digitalmars-d mailing list