Class/struct invariants
bearophile
bearophileHUGS at lycos.com
Tue Jun 15 18:29:27 PDT 2010
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
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list