strange behavior of by-value function arguments in postcondition

bauss jj_1337 at live.dk
Fri Sep 3 06:53:40 UTC 2021


On Friday, 3 September 2021 at 06:50:35 UTC, FeepingCreature 
wrote:
>
> Eh, I'd argue this shouldn't have an incondition. In my 
> opinion, the compiler would be justified here in removing the 
> exception, since you just told it, effectively, "x is always > 
> 10". So the `x < 10` branch is "dead code".
>
> But also, the `if (x < 10)` (really, `x <= 10`) test here is 
> *why* you'd be justified in writing `in (i > 10)` later.

You haven't told the compiler that x will always be > 10. You 
have told the compiler that you expect it to always be above 10 
but it might not be, say if the value came from user-input or a 
file somewhere in the call-chain. You have no way to guarantee 
that something always is something for the compiler, UNLESS the 
value can never come from a dynamic source. But in that case you 
might not need to make any guarantees because the value obviously 
will always be a compile-time constant and you could just 
calculate it using ctfe.


More information about the Digitalmars-d mailing list