-
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: Use ?<variable>
simplify handling of multiple-return-values
#33074
Comments
please leave the "if err != nil" alone ! |
Similar to #33029. |
I don't get this one: major, minor := http.ParseHTTPVersion("http1.1") ?ok os.Exit(1) In the examples with |
@ianlancetaylor ? means: major, minor := http.ParseHTTPVersion("http1.1") !ok os.Exit(1) |
There are a number of error handling proposals out there, such as check/try/catch, that are heavily modified at the language level or look ugly. I think the error returned by each expression should be taken seriously and should not be changed, but there is a real need to reduce |
@ianlancetaylor |
I love that the I am concerned that inlining the handling would make it harder to know the flow of a function.
Which would keep the ability to ignore indented code on a first pass of reading a function. See Mat Ryer's presentation on why he avoids |
The problem is not about |
@mvndaai yeah, maybe it looks confusing. // return err
r := os.Open(src) ?err return nil, err
// handle err
r := os.Open(src) ?err handle(err)
// handle then return
r := os.Open(src) ?err return nil, handle(err)
// example
io.Copy(w, r) ?err return nil, os.Remove(dst) |
This is basically #33029 with a slightly different syntax. This doesn't seem to bring any new functionality beyond that issue. This does not have strong support. Therefore, this is a likely decline. Leaving open for four weeks for further comments. |
I think the essence of simplifying error handling is to simplifying the handling of multi-return-value.
For example:
here, importing two special flag:
!var
and?var
! means:
assert(var != (zero value) )
if failed, then execute the
short circuit logic
.? means:
assert(var == (zero value) )
if failed, then execute the
short circuit logic
.One of the most common example:
The text was updated successfully, but these errors were encountered: