contract programming without a function

Ary Manzana ary at esperanto.org.ar
Mon May 21 05:49:30 PDT 2007


BCS escribió:
> 
> you can fake this with scope(exit)
> 
> 
> |assert(j == 0);
> |{
> |    scope(exit) assert(j == 10);
> |    for (i = 0; i < 10; i++)
> |        j++;
> |}

Almost. If your "in" or "out" bodies have statements that are not 
exclusively asserts, then they won't be gone when compiling in release mode.

The following would also be nice (althugh I seriosuly doubt people will 
use it):

| for(int i = 0; i < 10; i++) {
|     invariant {
|        assert(0 >= i && i < 10);
|     }
|     // some code...
| }

Here again, the invariant may contain code that is not exclusively 
assert, so in release mode it will be gone. And it's much nicer to the eye.


More information about the Digitalmars-d-learn mailing list