Improve "Improve Contract Syntax" DIP 1009

Nick Treleaven nick at geany.org
Sat Nov 4 13:02:45 UTC 2017


On Saturday, 4 November 2017 at 06:08:22 UTC, Jonathan M Davis 
wrote:
> Heck, take a really simply one like sqrt. All you have to check 
> in the out contract is the return value. You have no idea what 
> was passed in. So, how would you write an out contract 
> verifying that you got the correct number? If you also had 
> access to the input, then you could do the reverse operation by 
> squaring the result to see if it matched the input (assuming of 
> course that floating point rounding errors don't make that not 
> work), but you don't have access to the input.

I don't think I've ever written an out contract, so I am inclined 
to agree with you. However, there is a sqrt example for integers 
in the official docs, it does access its input:

https://dlang.org/spec/contracts.html#pre_post_contracts

long square_root(long x)
in
{
     assert(x >= 0);
}
out (result)
{
     assert((result * result) <= x && (result+1) * (result+1) > x);
}


More information about the Digitalmars-d mailing list