Contract programming syntax
Denis Koroskin
2korden at gmail.com
Wed Apr 8 14:18:39 PDT 2009
On Thu, 09 Apr 2009 00:51:49 +0400, Christopher Wright <dhasenan at gmail.com> wrote:
> 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
Does scope(exit) also make you feel that it is executed immediately?
I see nothing wrong with local variables to be available in "out", but in most cases "out" is defined /before/ function body so no variables will be visible this way.
It also looks like you can have multiple in{} and out{} sections this way. This is a nice bonus, btw!
More information about the Digitalmars-d
mailing list