-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: builtin: must function #32219
Comments
Have you seen https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md? The overlap is significant. |
This proposal is not about error handling (albeit weakly related). The emphasis is syntax sugar and flexibility (eg arbitrary function signatures). It's about the many occasions when you don't care about the error but just want to use the first result of a function directly in places without creating temporary variables. On other occasions, you do care about the error but want it converted to a panic so you catch it during development (i.e. the error is due to developer error), but during production, you assume no error is possible and you just want to first value directly. The standard library uses the convention Due to lack of generics, wrappers are used everywhere in the standard library and people's code due to different data types: Examples This could all be simplified with a generics-compatible builtin "must" function. (NB: builtin already has precedent of generics-compatible functions such as Another example where it's useful: An example from godoc encoding/json.Marshal
Sometimes you know that no error is returned because the struct is clearly json-compatible. It can be simplified to:
|
The function doesn't even need to be called |
CC @griesemer @mpvl |
I don't think that Go should promote converting errors to panics without first having better ways to handle errors. Errors in Go are well-known to be pretty cumbersome, having an easy way to convert errors into panics would just promote the idea that people shouldn't properly handle their errors. Now, if one of the several "named handler" counterproposals to the Go 2 Error Handling draft happen, someone could easily just make a handler called |
@deanveloper This proposal is not about error handling. If anything, this is about ignoring errors. The conversion of errors to panics is a necessarily consequence of the "inside" function potentially returning an error, yet the Also the conversion to panic has an additional benefit of alerting the developer of developer mistakes during local development. That is currently how it is used in standard package's scenario's such as https://godoc.org/html/template#Must and https://golang.org/pkg/regexp/#MustCompile. That is considered idiomatic go. The proposed function just assists developers write idiomatic go - at least for the scenario the standard package uses it with the |
@ianlancetaylor why is this a Go2 proposal? Can't it be a Go 1 proposal? |
Maybe not error handling in the sense of "what should I do with this error to handle it properly" but in the sense of "this function returns an error and I don't want to deal with it, how can I do this easily?"
Yes it is idiomatic for that instance, where the code for writing and using templates is already very verbose, and your program would cease to function if the templates were not done properly. The issue with adding a generalized The issue is that without better error handling, the Also the reason it's Go2 is because all language changes are being marked as such. More information here |
I disagree. If such a developer does such an action, then they are writing "incorrect" and bad code by ignoring the error they should not be ignoring. That is not the purpose of must. In summary, a good developer would only use the
|
I don't think that "that is not the purpose of must" exactly nullifies that lazy developers will use it as a substitute for error handling.
Again, a good developer sure, but Go is a simple language with a low learning curve. While good developers may understand how to use The reasons that
There's a reason that there's nothing like |
@pjebs All language changes are considered to be Go 2 proposals. But we are doing language changes. There will probably never be a literal "Go 2". See https://github.com/golang/proposal/blob/master/design/28221-go2-transitions.md . Separately, I agree with @deanveloper that regardless of the intent, this is in effect an error handling proposal. |
@pjebs is right that there are many cases where a function that can return an error won't do so if used in certain ways. In those situations, panic on error is correct, just as it is for slice index out-of-bounds, because there is a programming mistake. I read this as a proposal on cherry-picking a return value for use in expressions, so you can one-line things that currently require five lines:
See also Requirements to Consider for Go 2 Error Handling, section E.2. |
The emphasis is different plus it has a complimentary concept of My emphasis is simpler. #31442 is more an alternative pattern to error handling. His suggestion is Mine (due to different emphasis' such as inlining function calls that return multiple variables), My suggestion also renders the current His suggestion:
still requires it to be wrapped in a function (for the purposes of inlining). (But I could be wrong because maybe |
Also, my the intention behind my idea can be generalized as per @networkimprov as syntax sugar to cherry picking a return value. (I disagree with that generalization however because further adding the error => panic conversion seems to be clunky). #31442 idea can't be generalised as syntax sugar for cherry picking a return value. |
@griesemer re cherry-picking a return value, see #32219 (comment) |
Also my suggestion can be implemented as a function in "builtin". His suggestion as a statement is a language change. |
Even if #32437 is implemented, I think this |
Let's subsume this into #32437. It's a similar idea, with similar benefits and drawbacks. If we can't agree on error handling support (which might solve this issue as well), we aren't going to agree on this either. |
I've implemented a limited version of You can explore its usefulness. |
It would be great if there was a generic
must
function in "builtin" package.It would be equivalent to this:
the proposed
must( function() )
function will return x of type T1 iferr == nil
and panic(err) iferr != nil
The function is better suited for functions which return 2 values, the last of which is type
error
. But it can be generalized to any number of return values provided the final return value is of type error.The text was updated successfully, but these errors were encountered: