Ghost fields for Contract Programming
bearophile
bearophileHUGS at lycos.com
Sat Oct 9 13:29:46 PDT 2010
Tomek S.:
> You can do:
>
> class C {
> version(unittest) int temp;
> invariant() {
> // use temp
> }
> }
>
> Or am I missing something?
D2 has several ways to perform conditional compilation, so you may implement hand-made ghost fields using a version(debug). And ghost fields are very important to start with (I was not sure to submit an enhancement request).
There are two things that maybe you are missing. A field like:
version(unittest) int temp;
gets compiled when you use the -unittest, and gets removed otherwise. While the presence of ghost fields is determined by the presence of active contracts, that currently are run with the code is compiled in non-release build (so they are not related to the -unittest switch).
The second problem is that inside a method foo() of that class C you may write:
void foo() {
version(unittest) temp++;
}
But well implemented ghost fields are never visible inside the body of methods. So if you try to use them inside foo() the compiler has to raise a compile error.
Bye,
bearophile
More information about the Digitalmars-d
mailing list