-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Error handling check/with #49091
Comments
Some thoughts:
|
Thanks for the quick reply! My thoughts inline below
I think syntax highlighting helps here as well. One alternative is using single characters, which I know has some pushback, although it could bare similarity to
Yup, good catch. Even %%w, remains escaped in future calls so that wouldn't work. that solution would need to become: func formattedError(fmtStr string, args ...interface{}) func(err error) error {
return func(err error) error{
args = append(args, err)
return fmt.Errorf(fmtStr, args...)
}
} and the call to it:
That is correct. The intent was less to show "good code", and more to show how the semantics of this proposal would work |
|
Thanks for the suggestion, but based on the discussion above, and the emoji voting, this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
The proposal:
The overview of the error handling problem described here is spot on. The verbosity of error handling can make it tough to spot bugs. However, the design didn't allow for much flexibility in that the check used the most recent, in-scope handle. That doesn't provide a lot of flexibility to users. We can do better, by treating handle as a function (one that either takes a single error argument, or variadic args with the requirement the last arg is the error to check).
I propose we introduce 2 new keywords,
check
andwith
:When we check a return value, we can specify how we want to handle it, like so:
More information and examples in the template below
5+ YOE with Go in production/enterprise
Java, C, python, dart/typescript/javascript,
Easier, minimal new complexity, while readability is greatly increased
Yes, many error proposals exist
At this point, all of the various error proposals are slightly nuanced. I believe this proposal offers better flexibility in specifying the error handler, while maintaining clear readability.
Is this change backward compatible?
Show example code before and after the change.
See the example from the this previous draft's overview for the before.
After:
anything that parses go code.
Likely minimal. Not sure if this would cause issues with unbounded lookahead though
Depends on implementation/negligible
How would the language spec change?
As described
how does this change interact or overlap with existing features?
check would look similar to a return. Take some time to wrap our heads around that, but a welcome improvement to the current 3-liner.
No
Yup!
Again differences are nuanced but a cleaner syntax (IMO), and an expressive
with check
that allows flexibility in determining the handle function.Is this about
No
The text was updated successfully, but these errors were encountered: