Deprecate `!a == b`

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Aug 16 19:23:19 UTC 2024


On Tuesday, August 13, 2024 4:14:29 AM MDT Timon Gehr via dip.ideas 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).

I don't think that I have ever seen anyone do something like !a == b with
any type other than bool, and it would be annoying if it were disallowed for
bool. So, I don't really think that such a change would be particularly
valuable, but I also wouldn't particularly care if it were made an error in
cases where the types involved aren't bool. It wouldn't affect code that I
would write, and if it fixed the occasional bug, then that's arguably a good
thing. However, I definitely would rather not see this become an error for
bool.

- Jonathan M Davis





More information about the dip.ideas mailing list