Deprecate `!a == b`

Nick Treleaven nick at geany.org
Tue Aug 13 20:55:09 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 was a bit surprised that someone would write that (other than 
someone new to C-like syntax), but then I saw:
https://github.com/dlang/dmd/pull/16778#discussion_r1715017456

> 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).

Yes, it's consistent with other bug-prone patterns that dmd 
requires parentheses to disambiguate:
https://dlang.org/spec/expression.html#conditional_expressions
https://dlang.org/spec/expression.html#bitwise_expressions

Also braces for dangling `else`.



More information about the dip.ideas mailing list