Deprecate `!a == b`

IchorDev zxinsworld at gmail.com
Tue Aug 13 20:30:10 UTC 2024


On Tuesday, 13 August 2024 at 10:14:29 UTC, Timon Gehr wrote:
> A bug that crops up now and then in D is that someone negates 
> `a == b` by prepending a `!`. The result is `!a == b`. This 
> parses as `(!a) == b` and will often silently do the wrong 
> thing because negation implies cast to `bool`, and `bool` can 
> be compared with integral types and `enum` members.
>
> I think it would be better for this to give a diagnostic and 
> require explicit parentheses, similar to bitwise operators 
> (where the operator precedence is unintuitive in the other 
> direction).

Doesn’t require a DIP, but I’ve also never seen anyone think this 
would work? Do we really need to pander to people who don’t even 
understand that logical operators only *return* `bool`? Maybe 
that seems harsh, but I have never even thought of doing this 
because it’s just so obviously wrong—if I wrote it then I must 
have meant what I wrote, and I was probably happy with how it 
looked too. Having to wrap it in parenthesis would just negate 
that and add to my code’s parenthesis hell.

P.S. this isn’t a bug; and `!` is ‘logical not’, not ‘negation’.


More information about the dip.ideas mailing list