Contract programming syntax

Christopher Wright dhasenan at gmail.com
Wed Apr 8 15:38:07 PDT 2009


Denis Koroskin wrote:
> 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?

Well, take this example:

void foo(int i)
{
    if (i < 0) return;
    scope (exit) logExit;
}

The position has meaning here.

Then take this example:

void foo(int i)
{
    if (i < 0) return;
    in { assert (i != -1; }
}

This is confusing.

Good style dictates that your contracts should be separated from the 
body of your function, though.

The bigger issue is the apparent nesting of scopes.



More information about the Digitalmars-d mailing list