-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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 by assignment, with named handlers #32500
Comments
My main issue with this (and the reason I think that this has so many 👎 s) is because it relies heavily on symbols. Very few things in Go rely on unconventional symbols. The only one that I think Go actually uses (I may be wrong here) is the |
These four were new to me The cost of introducing a new symbol must be weighed against the benefit of cleaner error handling. (Note that a lot of gophers don't believe that eliminating If the designers proposed this, many would see it as a great advance; succinct yet noticeable :-) |
I'm not sure that authorship is the reason that people aren't liking it, don't blame it on that when there haven't been any responses. Whether or not you mean it this way, it comes off as incredibly selfish to blame others for not liking something that you made. (Selfish may not be the right word but I couldn't figure out a better one). Also, have you seen the +1 to -1 ratio, and the comments, on #32437? Just because designers propose something doesn't mean that people will like it.
In terms of the operators you mentioned - the I'll admit that
But what I'm getting at is that Go code can typically be explained with relative ease because it uses very few obscure symbols, nearly everything is typed out. I don't need to explain to someone what something like However, when we write out the full statement, it becomes quite obvious:
Now it reads as "we call os.Open, and panic if the error is not nil". No need to explain what weird symbols mean. I am someone who thinks that error handling is fine as it is, so take my words with a grain of salt. |
Gosh, I wasn't blaming anything on anyone. I was alluding to the high esteem the community has for the designers! IIRC, the early thumb count on |
Good to hear haha. Comments over text always come off differently as how they are meant. |
Added above: About that Pesky New Symbol People seem to dislike We could define three new keywords (for ops return, panic, invoke). They constrain the return value tested to the last one. To date,
I'd rather define one new keyword (or none) and see a symbol with an op that names its action. Dean, this is not addressed to you; please ignore it :-) |
Hey hey hey, no need to make it personal haha. I actually quite like it. I know there are a lot of compatibility arguments against something like that, but it really does look nice IMO. I sorta liked solutions like that in the other error handling proposal too. |
Having a special syntax to avoid explicit and in place error handling is IMHO a more way to postpone the problem, a more way to increase tech debt. |
this proposal suggests a Perlish style for Go and makes it tricky to work! |
I don't think I like this proposal, but I don't see why it needs a special character. You could just rely on the fact that x, return := F()
x, goto handler := F()
x, panic() := F() Of course it would be necessary to define what values are returned for the x, return fmt.Errorf("my error: %v", err) := F() One approach would be to use named result parameters, although that confuses the error result of All that said I think it's unlikely that we would adopt this. It seems too ad hoc for the problem that it solves. Speaking only for myself. |
This proposal creates a sort of special assignment destination: if you assign to one of these special destinations, then something different happens. We would have to understand how this works in package scope. We would have to understand the order of operations. Presumably the special destination can be used anywhere on the left hand side of an assignment, so what happens with #return, #return := F() or even #return, #return := F1(), F2()
#H1(), #H2() = F3() In short this seems to require quite a few more rules regarding evaluation order. We only have a few special characters left unused. Is this special purpose operation the right way to use one of them? This could probably be made to work, but it comes at a significant cost in complexity of assignment statements. Also the test against a zero value seems somewhat ad hoc, and seems to be the opposite of what one might want for non-interface values such as booleans. There doesn't seem to be much enthusiasm for this proposal. It's not clear that people want this kind of shortcut in the first place, as we've learned from other proposals in this space. -- writing for @golang/proposal-review |
Background
The feedback wiki for the Go 2 Draft Design proposing check/handle saw the emergence of two recurring themes, documented here:
https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback#recurring-themes
13 responses suggested a nil test triggered by assignment, to reduce
if err != nil
boilerplate:17 responses suggested named error handlers, to enable multiple error paths within a function:
Others have newly reiterated both themes in comments on the
try()
proposal #32437.Herein is a composite of these recurring themes. I believe it bears consideration in light of the apparently deep resistance to
try()
.Summary
v, flag [op] := f()
try v, flag [op] := f() // alternative, to indicate control-flow within statement
e.g. return, panic, or a named handler (defined at package-level or in current function)
Possible syntax (choose one)
Possible ops
Example handlers
A sub-expression syntax (which has caused complaint in the
try()
comments) is possible, but only recommended for ignore or panic ops:About that Pesky New Symbol
People seem to dislike
#op
or@op
or?op
, so let's ponder an alternative.We could define three new keywords (for ops return, panic, invoke). They constrain the return value tested to the last one. To date,
try
&must
have been suggested for return & panic, and we could employcheck
for invoke. One can already use_
for op ignore, although it looks like a bug, and blank-assigned values can't be logged for debugging.I'd rather define one new keyword (or none) and see a symbol with an op that names its action.
Work in Progress
As the
try()
proposal may be adopted, I leave this proposal in an incomplete state. I may develop it further if the Go team expresses interest.More possibilities are described in Requirements to Consider for Go 2 Error Handling, but most of them are not "recurring themes".
Who's @networkimprov? Liam is the author of
https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback/_history
The text was updated successfully, but these errors were encountered: