contract programming without a function

Jan Hanselaer jan.hanselaer at gmail.com
Sun May 20 00:35:46 PDT 2007


"Tyler Knott" <tywebmail at mailcity.com> schreef in bericht 
news:f2ogbj$2kp6$1 at digitalmars.com...
> 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).

Actually I know all this, and I've been using it this way. Maybe the example 
I gave was a bit misleading but what I actualy want to know is if it's 
possible to use these pre- and postconditions somewhere else then with a 
function body. Because in the manual they state that with a funtion is "the 
most typical use", so I'm asking myself what the other uses could be.

Jan 




More information about the Digitalmars-d-learn mailing list