contract programming without a function

Tyler Knott tywebmail at mailcity.com
Sat May 19 20:47:13 PDT 2007


You just got the syntax of the contracts slightly wrong.  This is the correct 
version:

bool fooFunc(Foo* f)
     in
     {
         assert(f !is null);
     }
     out (result)
     {
         assert(f.fooWasDone());
         assert(result == true);
     }
     body
     {
         bool fooVal;
         fooVal = f.doFoo();
         return fooVal;
     }

Note that the in, out, and body are not wrapped in curly braces and that the in 
and out contracts can't access variables from the body contract (because they 
are in different scopes), though the in and out contracts can access the 
arguments and the out contract can also access the return value (as an optional 
parameter passed to it).  Finally, keep in mind that the in and out contracts 
(like asserts) aren't included when the compiler is set to release mode so don't 
put critical error checking in them (they're intended to be used to help verify 
program correctness while you're developing the program).


More information about the Digitalmars-d-learn mailing list