-
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: Go 2: operator to cause early function return on error #32601
Comments
This appears to be covered by #32500 |
But what about wrapping errors? |
This is pretty. I have one question about your phrasing to be certain of understanding. When you wrote:
and mentioned "last value would be used" you mean this:
May be some debate about reserving the '?' for other uses but the code examples sure look nice. |
I don't know whether this has been suggested before - frankly I have lost track of what has been suggested in the error handling saga! - but I agree with @MichaelTJones that the syntax is easy on the eye and it would be backwards compatible as As far as decorating errors is concerned, you could use However, I still prefer the
r := try(os.Open(src))
defer r.Close()
w := try(os.Create(dst))
defer func() {
w.Close()
if err != nil {
os.Remove(dst)
}
}()
try(io.Copy(w, r))
try(w.Close()) you'd have to write: r, ? := os.Open(src)
defer r.Close()
w, ? := os.Create(dst)
defer func() {
w.Close()
if err != nil {
os.Remove(dst)
}
}()
_, ? := io.Copy(w, r) // assignment statement needed here
? := w.Close() // and again here
|
This is one of the most common suggestions in the responses to check/handle: I filed a sophisticated variant of this in #32500, and another in #27519, both of which add named handlers -- also a common request, documented in the link above. Many folks, including Go team members, aren't keen to introduce a new symbol. I don't get why, as this is a clear way to simplify error checks without
And it's not confined to type error nor the last return value. |
For what it's worth, I think @griesemer addressed the concerns with adding new symbols in https://github.com/golang/proposal/blob/master/design/32437-try-builtin.md :
|
Ignoring the arguments for and against, when I compare the snippets in my previous post, I still feel more comfortable with |
Go is nicer to read than Rust or Bash. But it did introduce 3 operators and a dedicated symbol: This would add a dedicated symbol, for a case more common than any of the above. The only functional complaint I've heard is that it doesn't provide a subexpression. However, the fact that IMHO, a reasonable case for a subexpression is |
Proposal
This proposal aims to reduce the boilerplate of basic error handling by introducing a new identifier.
We will use
?
in this proposal, which will behave as follows: when a error is assigned to?
the function would return immediately with the error received.
As for the other variables their values would be:
The use of
?
would allow to use+=
and similar operators on the other value, as if?
was not there.When
?
receives an error the last value of the named value would be returned.Examples
Anonymous
From:
To:
Named
From:
To:
The text was updated successfully, but these errors were encountered: