Missing python-like chaining in D

Paul Backus snarwin at gmail.com
Tue Feb 22 14:23:19 UTC 2022


On Tuesday, 22 February 2022 at 14:13:38 UTC, Dom DiSc 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, [...]
>
> 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.
[...]
> 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!!

As far as I can tell Python does not actually evaluate the middle 
operand twice. Instead, it evaluates it once and stores the 
result in a temporary.

```
>>> def f():
...     print("f")
...     return 1
...
>>> def g():
...     print("g")
...     return 2
...
>>> def h():
...     print("h")
...     return 3
...
>>> f() < g() < h()
f
g
h
True
```


More information about the Digitalmars-d mailing list