Bug 11410 – Support equality operator chaining

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-01T07:35:00Z
Last change time
2013-11-10T15:41:26Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-11-01T07:35:12Z
This is valid C code: int main() { int x = 3; int y = 4; int z = 5; int b = x < y < z; return 0; } Bug GCC tells us that code doesn't work as expected by a Python programmer: test.c:5:15: warning: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses] int b = x < y < z; So D disallows that code: test.d(5): Error: semicolon expected, not '<' test.d(5): Error: found '<' instead of statement - - - - - - - - - - - - - - GCC accepts this code: int main() { int x = 4; int y = 4; int z = 4; int b = x == y == z; return 0; } With a warning: test.c:5:15: warning: suggest parentheses around comparison in operand of '==' [-Wparentheses] int b = x == y == z; While DMD refuses that code: test.d(5): Error: semicolon expected, not '==' test.d(5): Error: found '==' instead of statement From my experience with D code I know there several situations where the chained equality is handy: if (n == foo(n) == bar(n) && ... It avoids code like: aux = foo(n); if (n == aux && aux == bar(n) && ...
Comment #1 by bearophile_hugs — 2013-11-10T15:41:26Z
Closed down, for the reasons explained here by Meta: > I was re-reading TDPL recently, and Andrei explicitly mentions > this. The reason D disables this instead of allowing it is > because it was deemed a bad idea to allow the code to compile > with different semantics than the equivalent C code.