-
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: simplify error handling with ? and ... #33152
Comments
Second point looks good |
The |
Didn't see that issue. I've seen proposals to allow |
I was going to open an issue to propose the same thing as the 2nd point. I see two main benefits of this:
This proposal solves both issues beautifully. Compare func before() (bool, string, MyStruct, error) {
return false, "", MyStruct{}, errors.New("error")
}
func after() (bool, string, MyStruct, error) {
return ..., errors.New("error")
} Let's throw the "ellipsis" keyword here so that this issue can be found more easily, because I think that's what "..." is commonly called. Also, if #21291 passes, this could be used to return explicitly "all-zeros" with |
Hey, thanks for the proposal. |
Yes, I have come to the conclusion that apart from |
My gut feeling says that maybe it’s too early to solve this “problem” with the current state of the language. |
IMO those are 2 different proposals, for example I really like the |
Isn't named return values an alternate solution for ellipsis?
We don't have to explicitly return the zero values each time this way. They are auto populated at function start.
|
Named values may have been set and not zero. Depending on the function, that matters. Also they can't be used in all (or even most?) functions, which makes it inappropriate for an editor macro. |
A lot of times, it is needed to return whatever values are set when some error occurs. Just because an error occurs doesn't mean you should always return zero values for other return parameters. The calling code may inspect this error and take necessary action using the "intermediate" result. I wonder why it cannot be used in most functions. |
io.Reader/Writer is the exception that proves the rule. Almost nothing else can return both an error and a result. It's pretty much one or other. IO is different because it is possible to do a partial read/write and still get meaningful results, but there's no such thing as half an HTTP response header or half a decompressed file. |
Also "variadic return" |
@carlmjohnson I love the |
The
Looks like |
why not just |
@nigeltao in 2012: https://groups.google.com/forum/#!msg/golang-dev/iAysKGpniLw/qSbtBUx4-sMJ
var x struct{} = _
var y int = _
func f() (bool, string, io.Reader) {
c := make(chan struct{}, 99)
c <- _
return _, _, _
} |
The first part of this proposal, using I suggest that someone open a new issue to focus only on the |
@ianlancetaylor There's #21182 for ... in return. I retracted it, but don't mind if it's reopened. I closed it in favor of #19642 which is the issue for _ as a universal zero value |
@jimmyfrasche Thanks. I reopened #21182. |
The
try
proposal #32437 was recently declined. This is an alternate proposal to achieve the goal of simplifying error handling. It has two parts:EXPRESSION?
return a bool, true if the expression is a non-blank value, false is the expression equals the blank value. So, for interface types, it would be equivalent toEXPRESSION != nil
.Usage:
return ..., EXPRESSION
fill in as many blank values as needed to fulfill the signature of the function.Usage:
return ..., err
return ..., fmt.Errorf("something went wrong: %v")
Rationale:
Given the design goals for improving error handling, it is difficult to imagine any proposal that doesn't end up being non-orthogonal with
if
anddefer
. This makes it simpler for text editors to insert the correct boilerplate for an early return automatically, and it saves a few characters for programmers who type the whole thing out manually.The text was updated successfully, but these errors were encountered: