Missing python-like chaining in D

Abdulhaq alynch4047 at gmail.com
Mon Feb 21 11:23:39 UTC 2022


On Monday, 21 February 2022 at 10:27:08 UTC, forkit wrote:
> 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, nor do I 
>> have any desire of obtaining said clue.
>
> It's just a chain..
>
> just like...
>
> cat.meow().eat().sleep().play();
>
> which, the more verbose way, is:
>
> cat.meow();
> cat.eat();
> cat.sleep();
> cat.play();

I think you've misunderstood what this is doing in python. 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.

e.g. 2 < x < 5 means x > 2 and x < 5, i.e. x is between 2 and 5. 
It only applies to comparisons and it's a genuinely useful bit of 
syntactic sugar that is not confusing when used properly.


More information about the Digitalmars-d mailing list