-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: nil checks by default #21769
Comments
This goes against two of Go's strong points.
Your example focuses on |
Thanks, but my main intend is readability most of all, I really don't mind the typing. But in general following seems rather logical/natural/easy to read ...
... whereas I am seeing a lot of double negation (is some error not nothing). |
This has come up before and a related situation in C and C++, where a pointer can be a boolean, is notorious as the cause of many bad bugs, such as
Even if you restricted the idea to interfaces and not also pointers, which would be inconsistent, I'm sure this proposal would introduce similar problems, especially during refactoring. I am strongly disposed against this proposal. |
I find this a rather strong argument, it can really leads to bugs in languages like C/C++, PHP or Python. One possible solutionLiteral use of special non-bool variables like
But short variable declaration, might not be as dummy-friendly, don't know how one could tackle e.g.
|
Please don't. |
The most subtle version I've seen of the bug @robpike describes looks like this: var enabled = flag.Bool("enabled", false, "if true, break everything")
… // Some distance later...
if enabled { // seems pretty ok/hard to misuse[!?]
experiment.breakEverything()
} The bug is the failure to dereference the |
As the very first sentence of this proposal states, I would only allow this for non-bool variables/pointers. In my example
In your example the check is of type
As mentioned above I would simply not allow it for bool, *bool, etc., why should you? But within the scope I provided (1. no Please prove me wrong! |
Actually, quoting: "I propose to evaluate to nil checks automatically when a non-bool variable is used, and no comparison operatores with it, e.g.:" Pointer types are non-bool types. So given What was probably meant is to exclude types having underlying type bool and pointers to such types. Still, I see no improvement of the language by adopting the proposal. Quite the opposite. |
There are several counter-arguments above. Proposal declined. |
I propose to evaluate to nil checks automatically when a non-bool variable is used, and no comparison operatores with it, e.g.:
Currently
Right now it defaults to a "non-bool err (type %T) used as if condition" compile error.
Reason
Correct me if I am wrong, but it seems easier to read/understand than the usual
if err != nil
, especially considering the amount it is being used. An option to leave the nil-check out, seems very appealing to me.The text was updated successfully, but these errors were encountered: