Bug 5024 – Order of execution of invariant and pre/post conditions

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-10-09T06:36:00Z
Last change time
2012-01-19T19:28:04Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-10-09T06:36:30Z
This is a D2 program that uses Contracts: import std.c.stdio: printf; class Foo { int x = 0; invariant() { printf("Foo invariant: %d\n", x); assert(x >= 0); } this() { printf("Foo constructor: %d\n", x); x = 0; } void setX(int newx) in { printf("Foo.setX precondition: %d\n", newx); assert(newx >= 0); } out { printf("Foo.setX postcondition: %d\n", x); assert(x == newx); } body { printf("Foo.setX body\n"); x = newx; } } void main() { auto c = new Foo(); c.setX(10); } This is the output of the program, DMD 2.049: Foo constructor: 0 Foo invariant: 0 Foo.setX precondition: 10 Foo invariant: 0 Foo.setX body Foo invariant: 10 Foo.setX postcondition: 10 But I think the Foo.setX precondition needs to run after the Foo invariant, and the Foo.setX postcondition has to run before the Foo invariant. See bug 5023 See also bug 3578 and bug 3856
Comment #1 by bugzilla — 2012-01-19T13:58:09Z
*** This issue has been marked as a duplicate of issue 5023 ***
Comment #2 by bearophile_hugs — 2012-01-19T19:25:11Z
It's not a duplicate. Issue 5023 asks to document something, and this issue asks to make that something in a different way. A discussion thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=155592
Comment #3 by bearophile_hugs — 2012-01-19T19:28:04Z
Walter has said: > My reasoning is it (1) doesn't make any difference and (2) it's always > been like that - and if it did make a difference, it would break 10 years > of existing code. A note: this issue is of a list of about twenty issues that I especially care about. Essentially *all* of them are little/tiny breaking changes like this one.