-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: spec: add zero-inequality operator ? #71320
Comments
I don't think the |
This (extended to all zeroes, maybe a vet check for bools) + #21182 would handle a lot of the most common rough spots around zero values and it would be nice to only have to read if err? {
return ..., fmt.Errorf("blah: %w", err)
} While that simplifies the boilerplate around the common cases of returning an error as-is or reacting to/decorating the error and returning, it's still 3 lines to read even if the important parts are more in focus, so I'm not convinced this is a better idea than something that (somehow) does just those 2 things on the same line. Checking if something is zero looks a bit busy: |
You're right, there's no major gotchas and I must admit, with the edits in place, it becomes much more similar to past proposals and so may not be worth rehashing. The only difference would be time, as the community seems more open to introducing a new |
Thanks, but closing as a dup of #32845. |
Proposal Details
Background
The Go community has a wealth of opinions on error handling: some are indifferent, some are annoyed, and some want to watch the world burn (#71203[1]). The goal of this modest proposal[2] is to surface a common middle-ground to see if there's any light to be found.
This is a modern generalization of #32845, and aside from the existing error handling proposals, relates to #64825.
Proposal
A new post-fix operator
?
evaluates for inequality with the zero value forcomparable
s. For exposition, the following are equivalent:The benefits include:
== zero
equivalent, so it make those if-statements stand out more.Sugar
Behavior can be emulated today with:
Unconventional cases that should be vetted:
[1] Said 100% in jest.
[2] But seriously.
[3] I don't type the spaces and let gofmt add those - OK, four characters.
[4] My editor auto-completes and I just hit tab - OK, fair enough.
The text was updated successfully, but these errors were encountered: