What's the use case of clear?

django django at thisinotmyrealemail.com
Tue Aug 10 07:49:16 PDT 2010


I read the recent discussion on the subject (TDPL: Manual invocation of
destructor) but I still don't understand what is the use case clear() wants to
cover?

Assume for a moment that we have this class:

class Connection {
	this() { connect(); }
	~this() { if (connected) disconnect(); }
	void disconnect() { ... }
	...
}

Now I see the following use cases:

// The connection is closed at some later point if at all.
var c1 = new Connection;


// I want the connection closed at the end of the function.
// Should be safe everytime.
void foo() {
	var c2 = new Connection;
	scope(exit) c2.disconnect();
	...
}


// I want the connection closed and memory released.
// (I know what I'm doing)
void bar() {
	var c2 = new Connection;
	scope(exit) {
		c2.disconnect();	// Even better, call the constructor.
		GC.free(c2);
	}
	...
}

What's left for clear()? In which scenario would I like the default
constructor called again?

Thanks


More information about the Digitalmars-d mailing list