Skip to content

Commit

Permalink
Merge pull request #699 from WalterBright/invariants
Browse files Browse the repository at this point in the history
don't run invariant for default construction
  • Loading branch information
MartinNowak committed Nov 21, 2014
2 parents aa03bff + d1c4110 commit 81fd583
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions contracts.dd
Expand Up @@ -149,7 +149,7 @@ $(H2 In, Out and Inheritance)
contracts.
)

$(H2 Invariants)
$(H2 $(LNAME2 Invariants, Invariants))

$(P Invariants are used to specify characteristics of a class or struct that
always must be true (except while executing a member function).
Expand All @@ -163,6 +163,12 @@ class Date
int day;
int hour;

this(int d, int h)
{
day = d;
hour = h;
}

invariant()
{
assert(1 <= day && day <= 31);
Expand All @@ -185,6 +191,10 @@ class Date
$(LI postconditions)
)

$(P The invariant is not checked if the class or struct is implicitly constructed using
the default $(CODE .init) value.
)

$(P The code in the invariant may not call any public non-static members
of the class or struct, either directly or indirectly.
Doing so will result in a stack overflow, as the invariant will wind
Expand Down Expand Up @@ -212,7 +222,7 @@ class Foo
}
------

The invariant can be checked with an <code>assert()</code> expression:
The invariant can be checked with an $(CODE assert()) expression:

$(OL
$(LI classes need to pass a class object)
Expand All @@ -235,7 +245,9 @@ assert(&s); // check that struct S invariant holds

$(P There can be only one $(I Invariant) per class or struct.)

$(P When compiling for release, the invariant code is not generated, and the compiled program runs at maximum speed.)
$(P When compiling for release, the invariant code is not generated, and the compiled program runs at maximum speed.
The compiler is free to assume the invariant holds true, regardless of whether code is generated for it or not, and
may optimize code accordingly.)

$(H2 References)

Expand Down

0 comments on commit 81fd583

Please sign in to comment.