proposal: Go 2: return error on assignment to _ #65345
Labels
error-handling
Language & library change proposals that are about error handling.
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
Go Programming Experience
Experienced
Other Languages Experience
Go, C, Scala, Kotlin
Related Idea
Has this idea, or one like it, been proposed before?
No
To be honest, I'm more hopeful about the tools the community develops for the proposal than about it being adopted as part of a new version of Go.
If the Go team do not appreciate the proposal, I hope somebody (including myself) in Go community can implement a tool for this proposal to avoid classic error handling in Go.
My implementation is not finished yet when I wrote this proposal.
Because if the needs of downstream developers (Go users) are not met, then we cannot count on upstream (Go team and libraries developers).
Does this affect error handling?
Yes.
Suppose there is a builtin or intrinsic function x accepts error only, tentatively designated as panic here.
Let the intrinsic function x be replaced to a ReturnStmt with zero value inferred by the compiler and the error passed to the caller in ReturnStmt.
Example:
to be:
And, it is not necessary to stick to this form, you can also provide a reserved identifier for such situation, which is more friendly to external tools based on Go AST.
Why?
Modifying Golang is breaking existing Go tools like toolkit, IDEs and attached components.
So the proposal try to support error handle in another implicit way without breaking them.
Until Go 1.21.6 version, there are only two paths of function exit in Go:
If you choose panic as the replacement of classic if err != nil, it will not break the check built in existing IDEs.
Is this about generics?
No
Proposal
Suppose there is a builtin function x accepts error only, tentatively designated as panic here.
New semantic to the x function: when the panic(v) parameter v is error, replace it with ReturnStmt, and let the compiler infer the zero value.
Add the BranchStmt check the implicit error if err != nil { return 0, err } after it.
Language Spec Changes
Informal Change
Classic:
Panic/recover:
This proposal:
Suppose there is a builtin function x accepts error only, tentatively designated as panic here.
Is this change backward compatible?
YES or NO.
No breaking change, but not backward compatible in such case:
But it can be avoided by reversed identifier
And it is also completely compatible with existing modern Go tools, IDEs when the x is panic .
So it DEPENDS ON YOUR CHOICE AND IMPLEMENTATION too.
Orthogonality: How does this change interact or overlap with existing features?
It is more simple than
Would this change make Go easier or harder to learn, and why?
What good it brings?
What complexity it brings? Users have to learn more about the new semantic of
Depends on choice and implementation.
Cost Description
This proposal strives to avoid changes in the language and language tool ecosystem due to the proposal.
Changes to Go ToolChain
gopls
Performance Costs
Compile cost depends on implementation. Runtime overhead is much smaller compared with panic/recover in Go std libraries, ha-ha-ha.
Prototype
Suppose there is a builtin function x accepts error only, tentatively designated as panic here.
Here is a reference implementation below. NOTE: pseudocode inside []
My implementation is not finished yet when I wrote this proposal.
I'm not familiar with the packages under cmd/compile/internal, I am discovering it while implementing the proposal.
The text was updated successfully, but these errors were encountered: