Contract programming syntax

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Apr 8 14:32:05 PDT 2009


Jarrett Billingsley wrote:
> On Wed, Apr 8, 2009 at 4:51 PM, Christopher Wright <dhasenan at gmail.com> wrote:
>> 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.
> 
> OK, then what about:
> 
> void foo()
> in { ... }
> out { ... }
> { ... }
> 
> the 'body' keyword is completely arbitrary.  There is no ambiguity here.

Agreed.

> Also, I almost never use contracts because of their verbosity.  It's
> much shorter - and functionally equivalent - to just put asserts in
> the function body, some at the beginning and some in a scope(exit).

Not technically true -- contract can contain more than raw asserts.
For example, it might contain a non-trivial loop (such as a foreach which calls 
a virtual opApply) which may be hard for the compiler to optimize out even if it 
can deduce the body is a no-op (because it only contained assertions, most likely).



More information about the Digitalmars-d mailing list