Contract programming syntax

Christopher Wright dhasenan at gmail.com
Wed Apr 8 13:51:49 PDT 2009


bearophile wrote:
> But isn't a syntax like the following better? To me it looks more logic, because in{} and out(){} are part of the function, and there's no need of a special syntax for the body (and the 'body' keyword):
> 
> long squareRoot(long x) {
>     in {
>         assert(x >= 0);
>     }
> 
>     out (result) {
>         assert((result * result) <= x && (result+1) * (result+1) >= x);
>     }
> 
>     return cast(long)std.math.sqrt(cast(real)x);
> }

No. This proposed syntax change is quite misleading. Contracts cannot 
access the function's local variables, but it looks like they can. 
Contracts are executed at particular times, but that syntax makes them 
look like they execute wherever they are written.

I believe you can put "body" before each function body, even with no 
contracts, if it makes you happier.

> Bye,
> bearophile



More information about the Digitalmars-d mailing list