Closed hherman1 closed 2 years ago
I hope I'm not wasting your time! I've read many of the error handling proposals over the years, and this has been bouncing around in my head for a while. I don't think I've seen something exactly like it, yet. Hopefully I'm not wrong.
See #40432 for a meta-issue. This seems similar to #32611.
Oh, ya that's nearly identical to what I'm proposing here. I guess what I've proposed incorporates critique 1 from that proposal. However, if reducing boilerplate is inadequate for this kind of language change (which seems fair to me), then I don't think my proposal is meaningfully different.
I'm going to leave the proposal open so you might confirm that ^ this is true, but I'm happy to close this.
Thanks for the quick response
Duplicate of #32611
Author background
Would you consider yourself a novice, intermediate, or experienced Go programmer? Experienced
What other languages do you have experience with?
Java, Python, Haskell, C
Related proposals
Proposal
Add a new keyword, e.g
try
to be used as follows:In this case, if err == nil then try will return the values on the right of the colon. If err == nil, the routine proceeds. The
try
keyword would require it's argument to have typeerror
.Most of my error handling has the form:
It requires a bit of typing and visual space. It's not too bad, but it is a bit of a nuisance. The aim of this proposal is to change Go programming in precisely this one case, and nowhere else. If you have any sort of complexity in your error handling, you would continue to use the rest of the language to manage your errors. However, in this specific case you can save a bit of typing and a bit of vertical space in your function.
I believe these are the main complaints from those who are bothered by error handling, and they would be satisfied with this proposal.
A new keyword would be added,
try
, which would be used in the formtry [error]: [values...]
.[error]
would be filled by an expression evaluating to typeerror
.[values...]
can be filled by anything you could write afterreturn
. If[error]
evaluates to a non-nilerror
,[values...]
would be returned.In other words, this:
Is exactly equal to:
I can return to this question and add details if what I wrote above is at all compelling or original.
Use the
try
keyword as shorthand for simple error checking. Youtry
an error, and if it is not nil, your function will return everything you passed totry
.Costs
if err != nil
pattern.