-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Go 2: remove the "if" keyword and use "err?!" to avoid error handling boilerplate #33067
Comments
We can't realistically remove the |
Cloud you show an example? |
I mean an example which is hard to be fixed by |
If we replace the |
Excessive simplicity may be a burden. |
this is like ruby's meta programming feature I agree on this:
|
Running |
I oppose. |
@ianlancetaylor, how about keeping the About the documentation problem, the change on map printing in Go 1.12 also has the same issue. |
Sure, we could just allow the new suggested approach. I was only pointing out that we can't realistically remove the The change to map printing affected fewer people than removing a key element of the language like |
@rocket049 Thanks for the note. I encourage you to either explain your position, or just use the emojis on the first comment to express your opinion. Please see https://golang.org/wiki/NoPlusOne , although in this case I guess it's really NoMinusOne. Thanks. |
Note that if x? {
...
} is equivalent to if x != zero-value-of-x {
...
} then values of type |
This is very interesting. I like the idea of having one way to know if any type is a zero value or nil. I would probably prefer a built-in function called |
In the upcoming 1.13 release we do have |
@ianlancetaylor
|
The |
I'll be honest, I'd rather have ternary. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I would already be happy with making either nil falsy everywhere oder just for the error type.
This would already remove most of the error checking boilerplate and is very simple and straight forward. Even this would be simpler to write but not harder to read:
|
This proposal does not add anything to the language we cannot already do. And it's not all that much shorter. And it's potentially confusing. And it has very little support. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
There were no further comments. |
This proposal looks ridiculous, but I think it has some good points, so I post it here anyway.
The proposal proposes that the if-block
should be simplified as
More generally, condition values can be of any type, instead of boolean types only:
which is equivalent to the current if-block as:
In other words, the
if
keyword becomes useless andcondValue
is equvalent tocondValue != zeroValueOfTypeOfCondValue
. This really makes the proposal look ridiculous, but I think, currently in Go 1, the if-block and switch-case-block have too much functionality overlapping. In other words, I think theif
keyword is not very essential for Go. This is one reason of removing theif
keyword.The other reason of removing the
if
keyword is we can use the following lineto simplify
to avoid error handling boilerplate.
[update 1] To avoid breaking the current semicolon insertion rules,
err? !
can be replaced withor
[update 2] Per Russ's comment,
condValue
is equvalent tocondValue != zeroValueOfTypeOfCondValue
can be limited to boolean and error values only, or just excludes string values.The text was updated successfully, but these errors were encountered: