Bug 6453 – Allow multiple invariant per struct/class
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-08T03:19:00Z
Last change time
2016-10-01T11:44:47Z
Keywords
contracts
Assigned to
nobody
Creator
simendsjo
Comments
Comment #0 by simendsjo — 2011-08-08T03:19:03Z
I would like to use a template mixin to add some fields to a struct, but I'd also like the template to add additional invariant checks without having to remember to add this for all struct/classes that mixes in this code.
class C {
int a;
}
mixin template EmbedC() {
C _c;
// oops.. more than one invariant
invariant() {
assert(_c);
}
void close() {
_c = null;
}
}
struct S {
int i = 10;
invariant() {
assert(i >= 10);
}
mixin EmbedC!();
}
void main() {
S s;
s.close();
s._c.a = 10; // access violation, but I want assertion from invariant
}
Comment #1 by yebblies — 2012-01-28T20:41:25Z
The spec explicitly states that there can only be one invariant, so this is an enhancement.
On the other hand, this appears to be an arbitrary restriction.
I completely agree with this change; I ran into this exact limitation for the exact same scenario as well. It's rather crippling for template mixins.
While on this topic, see also bug #5038. It would be a natural extension of this enhancement IMHO.
Comment #4 by yebblies — 2013-01-16T06:28:15Z
*** Issue 5038 has been marked as a duplicate of this issue. ***
Comment #5 by github-bugzilla — 2013-05-12T19:17:40Z