Class/struct invariants

bearophile bearophileHUGS at lycos.com
Wed Jun 16 05:08:53 PDT 2010


Steven Schveighoffer:
> 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.

Thank you for your answers, I was trying to understand.
Of all the examples I have shown this can be the worst:


struct Foo {
    int x;
    invariant() { assert(x > 0); }
}
void main() {
    Foo f = Foo(-10);
}

Here I'd like the compiler to assert (at compile time or at runtime), or to refuse an invariant in structs like that, where I think D has no way to enforce it (unless you call __invariant(), but this is silly).
Later I can write a "bug" report about this.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list