Presently a class invariant is required to be a single function:
class C {
int height;
int width;
invariant() {
assert( height < 10 );
assert( width < 11 );
}
}
For large and/or complex classes, it would improve locality if the invariant could be specified piecemeal, in much the same was that unittest blocks are stitched together into a whole:
class C {
int height;
invariant {
assert( height < 10 );
}
int width;
invariant {
assert( width < 11 );
}
}
The parentheses after invariant seem useless, too - also very much like unittest.
Comment #1 by bearophile_hugs — 2010-10-10T15:34:59Z
About invariant syntax see also bug 3856
Comment #2 by yebblies — 2013-01-16T06:28:15Z
The parentheses are necessary, because without them invariant is a deprecated alias for immutable.
*** This issue has been marked as a duplicate of issue 6453 ***
Comment #3 by bearophile_hugs — 2013-01-16T10:01:17Z
(In reply to comment #2)
> The parentheses are necessary, because without them invariant is a deprecated
> alias for immutable.
Eventually that alternative meaning will go away, so invariant for structs/class instances will be free to drop the useless ().
Comment #4 by yebblies — 2013-01-16T16:41:39Z
Fair enough.
Comment #5 by andrej.mitrovich — 2013-01-16T16:46:22Z
(In reply to comment #4)
> Fair enough.
The deprecation page lists it as deprecated since 2.057, but the Error and Gone dates are empty. I'm not sure what the plan is, but I doubt there's any *compilable* D2 code out there that actually uses invariant instead of immutable.
Comment #6 by yebblies — 2013-01-16T16:55:15Z
(In reply to comment #5)
> The deprecation page lists it as deprecated since 2.057, but the Error and Gone
> dates are empty. I'm not sure what the plan is, but I doubt there's any
> *compilable* D2 code out there that actually uses invariant instead of
> immutable.
The general idea is to leave at least six months between deprecation stages. The version numbers are missing because there is no fixed release schedule, and no guarantee the change would be pulled in time to match any projected date.
Comment #7 by clugdbug — 2013-01-17T01:28:22Z
(In reply to comment #6)
> (In reply to comment #5)
> > The deprecation page lists it as deprecated since 2.057, but the Error and Gone
> > dates are empty. I'm not sure what the plan is, but I doubt there's any
> > *compilable* D2 code out there that actually uses invariant instead of
> > immutable.
I am 100% sure that is true. 'invariant' only meant 'immutable' for a very short period of time, when hardly anyone was using D2, and when D2 was completely experimental and not seriously usable.
>
> The general idea is to leave at least six months between deprecation stages.
> The version numbers are missing because there is no fixed release schedule, and
> no guarantee the change would be pulled in time to match any projected date.
Well, this is a very strange situation. invariant without parentheses is LEGAL IN D1! When converting D1 code to D2, you get this nonsense deprecation message asking you to use immutable instead.
We have existing production code which uses invariant without parentheses!
We should make it legal again. This would be an undeprecation, which AFAIK has never happened before in D.
Comment #8 by dmitry.olsh — 2018-05-18T10:13:35Z
This compiles today on DMD v2.079.1:
class C {
int height;
int width;
invariant {
assert( height < 10 );
assert( width < 11 );
}
}