Missing python-like chaining in D

Abdulhaq alynch4047 at gmail.com
Tue Feb 22 14:24:46 UTC 2022


On Tuesday, 22 February 2022 at 14:13:38 UTC, Dom DiSc wrote:

> it means: eval each operator separately (so the operands in the 
> middle are evaluated twice), then AND all the resulting 
> booleans together and call that the result.
> But that is something completely different from what C does. 
> And D is meant to either do the same as C or don't compile the 
> construct at all.
> And as what C does is different from what math does, it was 
> decided to not allow this to compile  to avoid code to silently 
> change its meaning.
>
> I fully agree with this decision because evaluating an operand 
> twice is a behaviour that could have very bad side effects.  
> E.g. think of this:
>
> if(1 < (x++) < 2)
>
> Evaluating the middle operand twice will increment x by 2!! Is 
> this desired?! And even if it is, will it be incremented first 
> after checking if it is greater than one or after checking it 
> if it is smaller than two? The result may be different, and as 
> of yet there is no rule for this defined! Which one should it 
> be, and why?

No, the operand is not evaluated twice, from the language 
definition 
https://docs.python.org/3/reference/expressions.html#comparisons, 
op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and 
... y opN z, except that each expression is evaluated at most 
once.




More information about the Digitalmars-d mailing list