Contract programming syntax

Nick Sabalausky a at a.a
Wed Apr 8 10:41:25 PDT 2009


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:gridbr$mv4$1 at digitalmars.com...
>I like contract programming, it helps me avoid bugs. This is an example 
>from the docs:
>
> long squareRoot(long x)
>    in {
>        assert(x >= 0);
>    }
>
>    out (result) {
>        assert((result * result) <= x && (result+1) * (result+1) >= x);
>    }
>
>    body {
>        return cast(long)std.math.sqrt(cast(real)x);
>    }
>
>
> 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);
> }
>
> Bye,
> bearophile

This is one of those things that has always kinda bugged me about the 
language, but I never really considered a big enough deal to bring up. But 
now that it has been brought up: Yes, I agree completely. 





More information about the Digitalmars-d mailing list