Why will the delete keyword be removed?

Michel Fortin michel.fortin at michelf.com
Wed Jul 14 14:47:23 PDT 2010


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!

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.

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.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list