Prevent default-initialised struct

grauzone none at example.net
Wed Jan 28 03:08:15 PST 2009


I think it would really suck to introduce special cases to forbid 
default initialization of structs. And assuming T was the type of the 
struct, what would T.init do? Or typeid(T).init()?

Use a class instead.

If you really need a struct, you could use a private field, that signals 
if the struct was properly initialized.

E.g.

struct Foo {
	debug private bool initialized;
	
	static Foo opCall() {
		Foo n;
		debug n.initialized = true;
		return n;
	}
	
	void foo() {
		assert (initialized);
		//do something useful
	}
}


More information about the Digitalmars-d-learn mailing list