-
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: handle statement to use a type switch expression #28344
Comments
Have you added a link to the feedback wiki? Note that many of its posts suggest various ways to invoke a handler by name, so I think it's likely we'll see named handlers in a future draft. Given that, different error types could be dispatched to separate handler blocks. In Requirements to Consider for Go2 Error Handling, I assume named handlers, and mention implicit type assertion in B-3.1. That allows:
|
Hi @networkimprov -
I agree this would be nice to support, but I prefer to group them all in a single block, as opposed to the analog to your example in Go today, which would be doing a type assertion guarded by the "comma ok" in an |
While this looks like a good idea, remember that |
@deanveloper - that's a good point, but I fail to see how using |
Well it seems like the purpose of |
What would you rather write? handle err.(type) {
case *foreign.Err:
// do something with *foreign.Err
case *another.Err:
// do something with *another.Err
case *different.Err:
// do something with *different.Err
} or if err, ok := errors.As(*foreign.Err)(err); ok {
// do something with *foreign.Err
}
if err, ok := errors.As(*another.Err)(err); ok {
// do something with *another.Err
}
if err, ok := errors.As(*different.Err)(err); ok {
// do something with *different.Err
}
|
Those two things are completely different. It's not a "would you rather write", it's that the second one is just more correct most of the time. A switch statement doesn't scan down the error chain, it only looks at the topmost error. If we want error chaining to become a standard, adding features to only look at the outermost error is not a good idea. |
I've said already in agreement with your desire to use |
This seems like a small bit of syntactic sugar wrapper around the suggested Also, type switches on errors seem fragile. If some lower function adds a layer of wrapping, the type switch will stop working. Calling This is still linked from the wiki, but seems unlikely to be adopted, so closing. |
I would like to see if a modification to the proposed error handling design would be possible or is of any interest to other Go programmers. Just as
switch
can be parsed as a*ast.SwitchStmt
as well as a*ast.TypeSwitchStmt
, I suggest that the same hold true for thehandle
statement.For example, the
handle
could optionally support:I think this would encourage more Go users to create useful
error
implementations, result in simpler debugging, and make it easier for programmers to write more reliable code.It's clear that a type switch could be used inside the handle block, but I think the ergonomics of lifting the syntax up to the handle level work better, and make it more clear. If both a type switch and simpler error handling semantics are needed in the handle block, the user could simply use the handle statement as originally proposed, supporting both styles.
Apologies if this has already been proposed. I didn't find a duplicate suggested here or in the wiki.
The text was updated successfully, but these errors were encountered: