Missing python-like chaining in D

Abdulhaq alynch4047 at gmail.com
Tue Feb 22 08:45:53 UTC 2022


On Monday, 21 February 2022 at 18:05:41 UTC, Ali Çehreli wrote:
> On 2/21/22 02:27, forkit wrote:
>
> >>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>
> > cat.meow().eat().sleep().play();
>
> That misunderstanding (or miscommunication) is reason enough 
> why such loose semantics should not be in any serious 
> programming language.
>
> So, Python is happy with the fact that the result of 1 < 2 is 
> sometimes Boolean but sometimes 2? (Note that in chaining, not 
> the result but one of the arguments is used; and the chosen 
> argument happens to be the one that's "close" to the next 
> operator. Ha ha! :) How convenient... And why would 'a > b < c' 
> not mean "a is greater than b and a is less than c" or "a is 
> greater than b OR b is less than c"? I can guess: They picked 
> the only semantic that could possibly be explained that could 
> work. And then they must have slapped on a rationale: "Checking 
> more than two conditions is very common in Programming 
> Languages." I found that inaccurate sentence copy-pasted in 
> multiple blog posts:
>
> - They must mean "more than one condition", not "two"
>
> - They mean "program", not "programming language"
>
> - They ommit the supposed domain where that claim may be true 
> (I might have found a use for this feature perhaps once, if 
> ever? Not sure...)
>
> I think this is an example of blind leading blind in that 
> community.
>
> Ali

It seems you (and most of the people in this thread) have 
misunderstood what this is doing in python. 1 < 2 always returns 
a boolean, except in the case of operator overloading.

2 < x < 5 is implemented as ```(2 < x) and (x < 5)```, not as 
```(2 < x) < 5```


More information about the Digitalmars-d mailing list