www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Contracts for delegates

reply "bearophile" <bearophileHUGS lycos.com> writes:
How do you add contracts to delegates?

This doesn't work:


void main() {
     void delegate(int) foo;

     foo = (x)
     in {
         assert(x > 0);
     } body {
     };
}


Nor this:


void main() {
     void delegate(int) foo
     in {
         assert(x > 0);
     };

     foo = (x) {};
}

Bye,
bearophile
Jan 08 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 08.01.2013 12:14, schrieb bearophile:
 How do you add contracts to delegates?

 This doesn't work:


 void main() {
      void delegate(int) foo;

      foo = (x)
      in {
          assert(x > 0);
      } body {
      };
 }


 Nor this:


 void main() {
      void delegate(int) foo
      in {
          assert(x > 0);
      };

      foo = (x) {};
 }

 Bye,
 bearophile
I don't think that this is possible at all. Do you think this is usefull? Kind Regards Benjamin Thaut
Jan 08 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Benjamin Thaut:

 I don't think that this is possible at all.
I see.
 Do you think this is usefull?
In a program I define a null delegate, then later I assign to it one of different functions according to some run time values, and later I call the delegate several times. In such situation a delegate is handy to shorten my code, where max performance is not needed. I generally use contract programming, because I've seen it catches some of my coding mistakes. So what's wrong in desiring to add a pre-condition to that delegate? (Currently I have put the pre-condition asserts inside the function assigned to the delegate). Bye, bearophile
Jan 08 2013
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
08-Jan-2013 17:58, bearophile пишет:
 Benjamin Thaut:

 I don't think that this is possible at all.
I see.
 Do you think this is usefull?
In a program I define a null delegate, then later I assign to it one of different functions according to some run time values, and later I call the delegate several times. In such situation a delegate is handy to shorten my code, where max performance is not needed. I generally use contract programming, because I've seen it catches some of my coding mistakes. So what's
wrong in desiring to add a
 pre-condition to that delegate?
Then it has to be part of the type (a meta-info bound to it)... how would you then check if 2 functions have equivalent (but with different code) preconditions? (Currently I have put the pre-condition
 asserts inside the function assigned to the delegate).
Where it truly belongs.
 Bye,
 bearophile
-- Dmitry Olshansky
Jan 08 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Dmitry Olshansky:

 Then it has to be part of the type (a meta-info bound to it)...
In the first case the contracts are shared by all delegates, so there is no need for code equality: void main() { void delegate(int x) foo in { assert(x > 0); }; foo = (x) {}; } If the contracts are attached to the function, can't you just trust the programmers to use compatible contracts, so again there's no need to compare code? void main() { void delegate(int) foo; foo = (x) in { assert(x > 0); } body { }; } Bye, bearophile
Jan 08 2013