D's design by contract is missing "old"?

Russ Williams digitalmars.D.learn at russcon.removethispart.andthis.org
Mon Jun 18 11:30:19 PDT 2007


Manfred Nowak Wrote:
> So this seems an application for D's non existing scope guard
>   `scope(entry)'
I don't know about this nonexisting scope guard, but if it means the programmer has to write explicit code to do it, that's not the good way to do it...

> In fact pre- and postconditions seem to be nothing else than blocks 
> that are injected into the function body by `scope(entry)' and `scope
> (exit)' respectively.

They are a little more than that since there are implications for inheritance (a precondition can be weakened in a derived class, and a postcondition can be strengthened in a derived class, which means OR-ing the preconditions of base.f() and derived.f() and AND-ing the postconditions).

That inheritance aspect of DBC is defined in the language and done by the compiler so the programmer doesn't have to write boilerplate code to screw with it.  Similarly "old" should be defined in the language.  As it stands now, D discourages (to put it mildly) one from writing truly useful postconditions on functions which changes the object in a way dependent on the state of the object when the function is called.

E.g. a container class might have a postcondition including the following assertion:

void addFoo(Foo f)
out
{
    assert(fooCount = old(fooCount) + 1);
}

I really don't want to have to write some kind of extra boilerplate code to save the starting value of fooCount in order to express such simple postconditions.


More information about the Digitalmars-d-learn mailing list