[Mono-list] Design by Contract

Joshua Tauberer tauberer@for.net
Tue, 28 Dec 2004 20:31:14 -0500 (EST)


> I've been using C# for quite awhile now, but one feature that I wish was
> available in the language is support for Design by Contract:
> automatically-enforced preconditions and postconditions on functions, and
> invariants on classes.
> ...
> Does anyone have an idea about how this could be done in a reasonable
> fashion? If so, would anyone be willing to help on the project?

Hi Stephen,

I've thought about this a few times and even tried hacking some
rudimentary DBC support into Mono using attributes.  The difficulty in
this depends on what you want out of DBC.  I was going for something that
would see contract violations at compile time (which turned out to be way
over my head, so I didn't get far).  Just adding assertions automatically
would be much easier, or even easy.

Adding pre/post-condition assertions would just mean going into the parts
of mcs for methods and properties, looping through the attributes on the
method/parameters, and adding some object creations (to instantiate the
attributes) and method invocations (to have the attribute perform the
check) in the generated IL.  Not much, really.

Getting something more powerful I found to be a lot harder (and I learned
from this why some people like languages with immutable variables).

- Josh Tauberer