+ 16
A class invariant is simply a property that holds for all instances of a class,
this is what i found from searching hope it can help you:
Invariant means something that should stick to its conditions no matter whatever changes or whoever uses/transforms it. That is to say, a property of a class always fulfills or satisfies some condition even after going through transformations by using public methods. So, the client or user of this class is ensured about the class and its property.
For example,
Condition on a function argument is that, it should always be > 0 (greater than zero) or should not be null.
Minimum_account_balance property of an account class states, it cannot go below 100. So all public functions should respect this condition and ensure class invariant.
Rule based dependency between variables, that is, the value of one variable depends on another, so if one changes, using some fix-rule, the other must also change. This relationship between 2 variables must be preserved. If it does not, then invariant is violated.
+ 4
ChillPill
thats really nice hack đ..
but what if we forgot to call invariant() after each method calls?
we should find a better way i believe ...
( đ€ seems too hard to do with c/c++)
nice to see D has something like this embeded into core..this already seems a most wanted feature
+ 3
Interesting. The concept of invariant sounds like a constraint on the internal state of an object.
I think such constraint makes sense when we have to deal with state mutation (and also during instantiation).
In Java I think this would commonly be encoded in a setter method... Which should fail or default if the constraint is violated.