www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ghost fields for Contract Programming

reply bearophile <bearophileHUGS lycos.com> writes:
A possible enhancement for Contract Programming:
http://d.puremagic.com/issues/show_bug.cgi?id=5027

This is not meant as a replacement for "old" (that allows to refer to the state
at the entry to the instance method).

Bye,
bearophile
Oct 09 2010
parent reply Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
bearophile napisał:

 The ghost fields may be used to store partial computations useful to reduce
the work done 
by the class invariant. You can do: class C { version(unittest) int temp; invariant() { // use temp } } Or am I missing something? -- Tomek
Oct 09 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Oct 09 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
 And ghost fields are very important to start with (I was not sure to submit an
enhancement request).
Sorry, I meant, 'And ghost fields aren't very important to start with'. Bye, bearophile
Oct 09 2010
prev sibling parent Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
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
Oct 09 2010
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 09 Oct 2010 23:51:44 +0400, Tomek Sowi=C5=84ski <just ask.me> wr=
ote:

 bearophile napisa=C5=82:

 The ghost fields may be used to store partial computations useful to =
=
 reduce the work done
by the class invariant. You can do: class C { version(unittest) int temp; invariant() { // use temp } } Or am I missing something?
How often do you *run* your application with unittests enabled?
Oct 09 2010