-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: one line error handling with EReturn function #43644
Comments
Please fill out https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing language changes |
see #32473 for a proposal on assigning to a handler functions As I understand the limitation on naming means you can only have a single handler per package? |
@seankhliao |
Are we talking about a local variable |
@deanveloper I don't tend to use global variables. |
Yes sorry for the confusion, it was because the proposal typically defines a global Still unsure what I think about it. |
@deanveloper Thank you for having the politeness of giving me your opinion. Based on what you said, I rewrote my proposal to make it as clear as possible. I believe you said in another thread that you like explicit "return " statements and I can completely understand this. |
Yeah I had said that before. I’m not sure if I still stand by that statement though. I’d rather use a solution that keeps explicit returns, but I’m getting more flexible to those that don’t have them. |
Introduction
I've read through a lot of the error handling proposals. I think the ones being considered are not simple. This proposal is for simple cases of assignment to an error. I do not propose any sort of global error handling. I do not propose any sort of chaining. I do not propose the mechanism proposed be provided to assignment to any variable.
Proposal
Any error function with the signature: func(error, string) error can be used in place of an assignment to a error variable. An error function is provided in the "fmt" package that will provide simple error handling. This error function is called EReturn.
Locally any function with the signature func(error, string) error can be used directly if it has the name eReturn Otherwise the function can be assigned to a variable called eReturn. Variable assignment could be used to assign different functions whether local or from a different package. It is my view that it would be unusual to have to more than one local error handler because the string parameter can be used to differenticate between use cases.
In use, the error is implicitly provided to the error function. Some examples may make the proposal more clear.
Assuming the package "fmt" is modified to include the following error function. Because it has the right signature and name, it can be directly used for many use simple use cases.
The proposed syntax with an error function in assignment position.
Both of the above case are syntactic sugar that the compiler can expand. For instance 2. is expanded to:
Locally the errstr passed through to the function may provide enough information to differentiate between cases. For example
Discuss
Possibly, EContinue, EBreak could be proposed to be used in loops. Possibly ELog could be used to continue
Template - Language change
this is an interesting question
Scala, Java, Some others
it's an extra concept to learn
I haven't looked at all the ideas in idea space
some of those concerned that simple error handling is too verbose
please see above proposal
please see above proposal
not my expertise
yes
see above proposal
vet, gopls, gofmt
I'm not a compiler writer
the proposal is syntactic sugar
an error function with specific name and signature
no interaction, I think
yes
there are a huge number of proposals, but I haven't read an equivalent
no
The text was updated successfully, but these errors were encountered: