On Tue, 15 Jun 2010 21:29:27 -0400, bearophile <[email protected]>
wrote:
Are D invariants supposed to be so "relaxed"? They don't get called with
default constructors:
struct Foo {
int x = 0;
this(int xx) { this.x = xx; }
invariant() { assert(x == 1); }
}
struct Bar {
int x = 0;
invariant() { assert(x == 1); }
}
class CFoo {
int x = 0;
this(int xx) { this.x = xx; }
invariant() { assert(x == 1); }
}
class CBar {
int x = 0;
invariant() { assert(x == 1); }
}
void main() {
Foo f1; // no asserts
Foo f2 = Foo(); // no asserts
// Foo f3 = Foo(0); // asserts, good
Bar b1; // no asserts
Bar b2 = Bar(); // no asserts
Bar b3 = Bar(0); // no asserts
//assert(b3); // can't be used
// b3.__invariant(); // asserts
//CFoo f3 = new CFoo(0); // asserts, good
CBar cb2 = new CBar(); // no asserts
//assert(cb2); // asserts, good
}
Default construction for structs is a weird animal in D. A struct can
always be default constructed and is always initialized to s.init. This
allows you to construct for instance an array of structs with simple
memory copying.
During default struct construction, no constructors are run (they aren't
allowed anyways) and no invariants are run. What would be the point of
running an invariant during default construction? The only think it could
possibly do is make code like this:
S s;
Fail without -release, and pass with -release. I don't see the value in
that.
-Steve