digitalmars.D.learn - Contracts for delegates
- bearophile (20/20) Jan 08 2013 How do you add contracts to delegates?
- Benjamin Thaut (4/24) Jan 08 2013 I don't think that this is possible at all. Do you think this is usefull...
- bearophile (14/16) Jan 08 2013 In a program I define a null delegate, then later I assign to it
- Dmitry Olshansky (9/23) Jan 08 2013 Then it has to be part of the type (a meta-info bound to it)... how
- bearophile (23/24) Jan 08 2013 In the first case the contracts are shared by all delegates, so
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
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, bearophileI don't think that this is possible at all. Do you think this is usefull? Kind Regards Benjamin Thaut
Jan 08 2013
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
08-Jan-2013 17:58, bearophile пишет:Benjamin Thaut:wrong in desiring to add aI 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'spre-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-conditionasserts inside the function assigned to the delegate).Where it truly belongs.Bye, bearophile-- Dmitry Olshansky
Jan 08 2013
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