-
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: go2 - Expand check functionality with scoped handle blocks #33002
Comments
This just seems like a mimic of java error handeling with try catch, but just renaming it. I think this has been covered before, not this exact proposal, but try catch. |
It can be seen as such, however it also allows for just adding the check keyword to multiple functions and handling them in a similar way. Finally, I do prefer the scoping of a try / catch in other languages, as it is clear what is happening.
|
When using a |
The idea would be that a check block would be a scope where all the functions are checked. Otherwise all normal operations are allowed, however, to stay consistent with the golang spec, everything declared within the I added more functions in the initial examples (first 2) to showcase how functions would be handled. |
That does suggest that adding an error result to an existing function will cause a silent behavior change. If the |
Would you suggest to require that all functions in a check block send an error at the end? Alternatively we can suggest that every function inside a check block still requires the
Good catch on the silent behavior. |
My main concern with this is that it disrupts the ability of happy path not being indented. Currently, if you don't use else understanding a function's happy path just means ignoring all indented code. My other concern is having multiple statements in one check block. Some developers would probably put the entirety of a function within a check block. That would make the code look simpler but changes the philosophy of dealing with errors when they happen, which is my favorite part of go. It makes my code feel safe. |
I don't like adding more than one keyword. |
This proposal does not have strong support. It's concerning that a change in function signature can cause a change in behavior at the call site, possibly skipping other functions in the When entering the For these reasons this proposal is a likely decline. Leaving open for one month for further discussion. |
There were no further comments. |
Based on the current proposals and counter proposals I would propose a revision of the
check/handle
proposal. Proposals can be found here: Go2 Error Handling FeedbackMy proposal is to add the option to have fully scoped blocks as well as requiring the handle to scoped, and enforce the variable assignment already used in golang. Finally the handle would move to the bottom, to maintain the logic of top to bottom programming logic (which the original check/handle breaks).
A simple example could be the following:
Alternatively you can also write the following:
There can be a discussion about omitting the handle part which would automatically translate to the following:
handle err { return err; }
- however, I prefer to be a bit more explicit and demand that every check has at least one handle assigned to it.If we take a look at a common used example below:
This can now be rewritten as such:
or alternatively, depending on your preference:
or a mix of oldskool + proposal:
This makes generic error handling (non-lazy as opposed to:
if err != nil { return err; }
) a lot simpler. Furthermore it's clean for the people reading the code since this is perfectly scoped.As such the requirements are the following:
check
keyword is followed by either{
, a function that returns an error as last parameter, or a variable of typeerror
handle
keyword is followed by a variable name, or_
for unused variable namecheck
) go STRAIGHT to the next handle block, in order of top to bottom, or "bubble up" in the scopehandle
always picks up an error type, it is either straight jumped to by thecheck
as long as it is within its scope or an error can just continue down there.The goal is to make the codebase repeat itself less while maintaining a clear flow of function handling, while giving the developers the possibility to handle the errors in whichever way they want.
The current
try
proposal just enables lazy error handling and without other additions to the language it provides a single way to attempt to fix a complex issue, while taking away the developers' choice on how to handle the error(s).Discuss away, if you vote, please leave a reasoning as to why.
The text was updated successfully, but these errors were encountered: