proposal: Go 2: syntactic sugar for return fmt.Errorf #56159
Labels
error-handling
Language & library change proposals that are about error handling.
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
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
I've read through a lot, but not all, of the existing error handling proposals. This one might have been proposed before and I missed it, but I haven't seen it yet.
It's smaller in scope than a lot of the solutions I read.
Yes
It is focused on exactly one type of error handling, not a broad class.
No
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
.I think so, but I'm not sure. I don't see how this could be parsed as valid go code in existing versions, so it seems like a purely additive change, and thus backwards compatible.
Show example code before and after the change.
This has a clear, very specific use case, in which it can be preferred to existing features. It is not usable in any other cases. Thus I think it avoids confusing the use of other features.
No
Costs
Harder to learn. We would introduce a new keyword, with an unusual syntax (would hope someone has an idea on how to make it prettier, or more familiar). It raises the question "why is this feature only for error handling?".
I believe the main cost is cognitive. It might also introduce churn as people rush to rewrite large swaths of perfectly fine error handling code. It also requires new muscle memory from devs, who are used to the
if err != nil
pattern.I suppose they would all need to learn how to handle this keyword, so presumably all of them.
I believe this should be extremely cheap at compile time, as it is a very simple translation.
I don't believe there is any additional run time cost in the cases where this will be used.
I don't know enough about the compiler to be useful here. I'm worried about how parsable this would be. But it seems like a simple thing to implement, if you can parse it. I can imagine how to do the AST generation.
No I don't.
The text was updated successfully, but these errors were encountered: