-
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: Error handling via conditional assignment operators #59664
Comments
@ianlancetaylor thanks for your work on managing the issues here. If you think this proposal has no likelihood of being accepted, please go ahead and close it to save everyone's time. As said I could not find a proposal that was 'close enough' to be a reference point and I thought the subtleties of this proposal were meaningful enough to be considered a novel avenue. |
If it's an assignment operator, it can't work with |
The effect is kind of like the |
Why not? It makes sense to disallow =. A() It's does look weird at first but it also does its job. Return on error or result in void.
True. |
@ianlancetaylor
Otherwise they indeed have the same effect. My primary intention was to bring up the idea that an extra assignment operator could also be used to reduce repetitive if-err checks as I have not seen it discussed. I feel like the actual chars used in the operator are secondary to how it works. |
I agree that I can't recall seeing a proposal quite like this.
I don't agree here. When people discuss the problem of error handling in Go, they can mean several different things, but one of them is a complaint about the syntax. This proposal, like many other error handling proposals, is about reducing the syntactic boilerplate for error handling. As such, the syntax is really the critical part. The syntax we all use today is verbose but clear. When we talk about new syntax, it will presumably be less verbose, but it must still be clear. We shouldn't say that the syntax is secondary. It's really the whole point of the proposal. |
In that case why not add the concept of aliases aliase ErrRet(e error) {
if e ! =nil {
return e
}
}
aliase ErrRet2(file io.Reader, e error) {
ErrRet(e)
//file rw from f
}
func useExample() error {
f, err := os.Open(filename)
ErrRet(err)
// file rw from f
}
func useExample2() error {
ErrRet2(os.Open(filename))
}
func useExample3() error {
f, err := os.Open(filename)
if err ! =nil {
return e
}
//file rw from f
} This is like other languages This does not affect the previous implementation, but also can streamline a lot of code. It might be possible to use, it's a reference to the existing alias definitions, but of course there might be some differences: type ErrRet = (e error) {
//do some error checking
} |
What I meant is that the chars of the operator could change if the functionality is otherwise desirable. But you are right, I proposed this because that was the best I could come up with and it's not like we could just improve it later. |
With a fresh mind, I'd like to reverse my response to @carlmjohnson . He is right, this is totally confusing. @ianlancetaylor is it okay if I close this? |
feel free to close it if you no longer think it's a good idea |
Thank you all for your time, I'd like to withdraw the proposal. |
Author background
Experienced.
TypeScript, PHP, C, C++, Java, Rust, Haskell, Pascal, Basic, Assembly.
Related proposals
There has been countless error handling proposals of course but I'm not aware of any that has introduced new assignment operators.
It introduces conditional assignment operators that either assigns the success values or returns the error value.
Yes.
It introduces new assignment operators.
No.
Proposal
=.
that=.
but the actual syntax could be different.:=.
).+=.
, etc..). That's not really important though, it just follows from the pattern.if err != nil { ... }
checks.if err != nil { ... }
checks in case one would not add debug information anyway.=.
.:=.
and optionally+=.
,&=.
, etc..=.
and:=.
would be added.+=.
and&=.
etc..If you just want to return on error, you can use the conditional assignment operator to simplify your code.
Yes.
I believe it is orthogonal. The benefit of using a suffix after the assignment char instead of a prefix is that it could be supported with all other assignment operators that are prefixes, though that is not crucial.
No.
Costs
It probably has a net positive impact. One additional feature to learn in exchange for code that's easier to read and faster to write.
I'm not sure about the scope of this question. Please clarify.
Probably most.
I would expect it to be negligible.
Zero as it compiles to the same underlying code one would write without this feature.
I'm not as deeply familiar with the Go codebase.
No.
The text was updated successfully, but these errors were encountered: