Missing python-like chaining in D
Dom DiSc
dominikus at scherkl.de
Tue Feb 22 14:13:38 UTC 2022
On Monday, 21 February 2022 at 09:48:53 UTC, Stanislav Blinov
wrote:
> On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:
>> It seems to me, that D is a language where python like
>> chaining would be right at home.
>>
>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>>
>
> I've no clue whatsoever how to interpret that mess, [...]
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?
More information about the Digitalmars-d
mailing list