-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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 Paradigm with ?err Grammar Sugar #60779
Comments
Please note that you should fill https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing a language change. |
Hi, I have fill the lost info. |
I change some things, it may be read clearer and implement easier Can you describe a possible implementation?? is a help func with return line
? = func (err *error) func(pos ...int) func(vars ...any) {
for _, i := range pos {
if e := vars[i]; e != nil {
*err = e
break
}
}
return vars
}
if err != nil {
return
}
var err error
a1, ?err, a1, ?err, err3 := int, error, int, error, error
-->
var err error
a1, _, a2, _, err3 := ?(&err)(1,3)(int, error, int, error, error)
// the below code for return is auto added by compiler
if err != nil {
return
} |
I have wrote a transpiler shynome/err4, here is effect source code package main
import "errors"
func main() {
var qTerr error
var qTerr2 error
_, _ = qTerr, qTerr2
qTerr = errors.New("e")
var e1, e2 error
_, qTerr, qTerr2 = 7, e1, e2
} after transpile with // Code generated github.com/shynome/err4 DO NOT EDIT
package main
import (
"errors"
"github.com/shynome/err4"
)
func main() {
var qTerr error
var qTerr2 error
_, _ = qTerr, qTerr2
_ = err4.Check(errors.New("e"))(&qTerr)
if qTerr != nil {
return
}
var e1, e2 error
_, _, _ = err4.Check2(7, e1, e2)(nil, &qTerr, &qTerr2)
if qTerr != nil {
return
}
if qTerr2 != nil {
return
}
} Writing golang is easy and happy |
I found the err := errors.New()
// replace err with !err, we can get if err != nil {return}
!err := errors.New()
// -->
err := errors.New()
if err != nil {return} // only fit with named return, a limit
//if you want do something with err, predefine it and do that in defer func
var err error
defer func(){ if err != nil {// do something here} }()
!err = errors.New() I want to close this, it's too similar with old proposal, why the old similar proposal had be closed? |
You may retract and close this proposal at any time. I think it is very unlikely that we would adopt this proposal. It is similar to several other proposals that we have not adopted. |
Would you consider yourself a novice, intermediate, or experienced Go programmer?
I think I am a intermediate Go programmer. I have write golang 3 years
What other languages do you have experience with?
Would this change make Go easier or harder to learn, and why?
this change make Go harder to learn, it has added a hidden return
Has this idea, or one like it, been proposed before?
If so, how does this proposal differ?
I make the err variable must is a defined variable, it make err handle easily and clearly
Who does this proposal help, and why?
All Go programmer who want write less code.
What is the proposed change?
It has add a Grammar Sugar
Please describe as precisely as possible the change to the language.
What would change in the language spec?
it add a operator
?
Please also describe the change informally, as in a class teaching Go.
Is this change backward compatible?
Yes, it keep backward compatible.
Show example code before and after the change.
What is the cost of this proposal? (Every language change has a cost).
How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
all tools would be affected, similar the change add generics support
What is the compile time cost?
a little compile time cost more than before
What is the run time cost?
no run time cost
Can you describe a possible implementation?
Do you have a prototype? (This is not required.)
Yes, I have wrote a transpiler shynome/err4
How would the language spec change?
it add a operator
?
Orthogonality: how does this change interact or overlap with existing features?
I don't know about this.
Is the goal of this change a performance improvement?
No, it is not.
Does this affect error handling?
yes, it is
If so, how does this differ from previous error handling proposals?
This proposal do not add another keyword, instead of it add
?err
(err must be a defined error type), it is a Grammar Sugar, it will not break old code.Grammar sugar is make program hard read, this sugar with a hidden return, but I think it is worth
Is this about generics?
No, it is not.
Updated: ?err move to left, it make things easily
The text was updated successfully, but these errors were encountered: