Ghost fields for Contract Programming

Tomek Sowiński just at ask.me
Sat Oct 9 13:53:08 PDT 2010


bearophile napisał:

> 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.

OK, thx for explanation. But as you said, ghost fields aren't that important and conditional 
compilation can provide a crude-but-working implementation, so I doubt the ROI would be 
positive.

-- 
Tomek


More information about the Digitalmars-d mailing list