proposal: Go 2: "escape statement" for error handling #57236
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
A lot of error checking in Go takes the form:
There are many attempts at no. 1 (#41908, #33029, #32848, #37243, #32848, #56895).
There are also attempts including no. 2 (the draft design, #56355, #21161, #18721 - most of these return zero values followed by the error).
This proposal suggests a new "escape statement" written
\ foo, bar
with the syntaxEscapeStmt = U+005C ExpressionList .
error
or typebool
Note that this syntax does not allow an empty ExpressionList, unlike the Return statement.
Allowing checks for boolean values also allows checking e.g.
value, ok := map[key]
map lookups.When formatting the code, the escape statement should be indented to keep the happy path left-aligned and read like traditional error handling. It should also be followed by a line break.
Example
This is os.File dirFS.join.
Before:
After:
Note that ok and err are not treated as unused values.
Note that even boolean expressions, like
fs.ValidPath(name)
, must be assigned to a boolean value first.(Some Qs elided as covered above)
Would this change make Go easier or harder to learn, and why?
Harder. Though easier to learn than the more advanced existing techniques for error handling.
How does this proposal differ [from those proposed before]?
Is this change backward compatible?
AFAIK, Go 1 programs would continue to work - backslash currently only appears in string or rune literals and is not valid, anywhere else.
What is the cost of this proposal? (Every language change has a cost).
Compiler changes, syntax changes, breaking tools, documentation updates, etc.
Orthogonality: how does this change interact or overlap with existing features?
Would benefit from #21182
Does this affect error handling?
Yes (sorry)
Is this about generics?
No
The text was updated successfully, but these errors were encountered: