surprising semantics of the && expression

Julian Fondren julian.fondren at gmail.com
Wed Feb 4 00:41:57 UTC 2026


On Tuesday, 3 February 2026 at 23:11:03 UTC, Iain Buclaw wrote:
> On Tuesday, 3 February 2026 at 17:29:05 UTC, user1234 wrote:
>> So yesterday someone oppened an issue:
>>
>> https://github.com/dlang/dmd/issues/22500
>>
>> Which was closed after accurate explanations of Ian. However 
>> I'd like to know where this semantics are coming from. I 
>> suspect C++ ? What is the history behind ?
>>
>
> This has been valid since early D1.

and only since DIP 1034 in 2021 can || still evaluate to a bool 
in an assert-like case:

```d
import std.stdio : writeln;
import core.stdc.stdlib : exit;

noreturn fail() {
     exit(1);
}

void main(string[] args) {
     bool b = args.length == 1 || fail();
     writeln(b);
}
```

>> Let's mention the POLA. The very basic human-being would 
>> expect that both the LHS and RHS sub-expressions can be 
>> implictly convertible to `bool`.

The principle of least astonishment most often comes across as a 
slightly rude dismissal of the expectations of others. What's the 
astonishment-math that says that another is wrong for expecting 
`test || fail()` to work in D? Now, you can do that if you don't 
take the value of the expression.

I imagine that the status quo silences some rare programmer 
errors, like

```d
bool flag;
test && complain();
do_something(flag);
```

where during editing you dropped an assignment to `flag`, and 
didn't notice because the code was still accepted with a 
different meaning than you intended. The code also would not have 
worked if you'd kept the assignment, but the two errors cancel 
out and permit a third error that you are now confused by: how 
could flag possibly be false when you can see the output from 
complain()???

The other way that the "principle of least astonishment" comes up 
is that someone has an unpleasant experience like this and wishes 
that the language hadn't permitted the confusion. Especially when 
the feature, that was used without the programmer even knowing 
about it, doesn't seem to add much. I sympathize a lot with this 
and have some stories, but you are just going to be confused 
sometimes, and terms like POLA too easily discount that reality. 
A new epoch of D with different enough semantics could lead to 
many errors of confusion just from the difference, even if the 
new semantics are much cleaner.


More information about the Digitalmars-d mailing list