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.
Comment #2 by yebblies — 2012-01-28T22:25:04Z
Comment #3 by alex — 2012-02-23T12:59:01Z
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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/ae70bfdeafd3a534e3f12747503bafa046be2f9d fix Issue 6453 - Allow multiple invariant per struct/class https://github.com/D-Programming-Language/dmd/commit/081c92a115b3006871c5a516267417e06ceacbcf Merge pull request #1978 from 9rnsr/multi_inv Issue 6453 - Allow multiple invariant per struct/class
Comment #6 by github-bugzilla — 2013-05-12T21:34:21Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/ce58435fe322afd736504f804bdad6a9debb8c1d Fix up for issue 6453 change, allow shared/synchronized invariants But mixing non-shared/shared/synchronized is currently rejected. https://github.com/D-Programming-Language/dmd/commit/4fbc77fa504c2d8ee4c7ef10edc70221a74742c3 Merge pull request #2028 from 9rnsr/multi_inv Fix up for issue 6453 change, allow shared/synchronized invariants
Comment #7 by github-bugzilla — 2016-05-01T12:40:48Z
Comment #8 by github-bugzilla — 2016-10-01T11:44:47Z