-
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: spec: error handling with block statement #68745
Comments
The return statement in If it did have return parameters, how would one know when an early return from the closing function is needed as opposed to the |
This is |
When facing an error, we have the following situations
3: Stop execution immediately and return to the current call
My proposal aims to solve the third problem |
I noticed that there was something wrong with my example, so I modified my proposal. Now instead of converting the function into a code block, I directly define a code block that can be called. You can take a look at
|
This proposal is a major change to the syntax (a new keyword, a new function declaration form) and semantics (functions that return values only on some paths, return statements that actually return from the caller function), and it defeats function encapsulation (since you have to know that a function contains non-local returns). Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
You're right, but so far all similar proposals have implicitly achieved this. On the contrary, I didn't evade the issue; instead, I explicitly proposed this feature. I think it's better to inform developers than not, and Of course, if you think Go doesn't need this feature, we can restrict its use to a very narrow scope (definitely not just for exception handling). Everything is open to discussion. However, my point is: I really dislike the approach of introducing a specialized structure solely for handling exceptions to solve exception-handling problems. I prefer a more natural way, such as introducing a new feature: block statement. block statement isn't born for error handling, but it naturally achieves that. Furthermore, block statement doesn't impose new learning burdens because Go already uses Lastly, what I want to emphasize is that many developers see Go as not very expressive. Therefore, it's good to enhance Go's expressiveness in this regard. of course, I know the goal of go |
In fact, the core problem of error handling is how to return, right? |
Go already has blocks, of course. And it already has functions. I'm not aware of anybody asking for the ability to write a block outside of a function, as this proposal suggests. The only additional feature that provides is the ability for a block to return from its calling function. That introduces a new hidden control flow--what looks like a function call can actually return from the calling function. That makes code harder to understand.
I don't think so myself. I think the core problem is how to reduce boilerplate code while retaining clarity and simplicity. |
As I mentioned above(#68745 (comment)), reduce boilerplate code is our goal, But when implementing this goal, the only problem we encounter is how can I reduce my code when I need to implement the following code, which is essentially a question of how to return
To solve the problem of error handling, any proposal cannot escape this question: how to return |
There is always resistance to introducing new features, but I think it is worth it, especially when the Maybe we can clearly distinguish block statement from functions when declaring them, so that developers can immediately understand their difference from functions. |
I think it would be much simpler to implement and use this request if you replace it with the concept of macros in C. previous discussion: #32620 |
There are a bunch of loose threads in this proposal. I'll walk through what my reactions were. I think that will show you what I mean.
Of course, you may have different ideas for where you would like to take the proposal. I am mostly trying to illustrate what you need to consider. |
See also #54361. |
Thank you for your thoughts. Here is my answer.
by the way there are many points to discuss, but the reuse of code blocks is the core of this proposal, I don’t want to only solve the problem of error-handling. |
I really understand your restraint, and that's why I love golang, but I also think it's time to give golang a little more expressiveness (currently |
Thank you. I read the proposal. Although the two proposals solve different problems, I think our ideas seem to be very similar. See, one feature solves many problems. |
I thought about your suggestion again and looked at this issue. Maybe I shouldn't reject this implementation,because I like this answer:#32620 (comment),>> But I still want to call it a block statement because block statement are easier to understand for people who don’t have experience in other languages. |
This is the code that is currently supported
What is the first reaction when people see if I don't use goto, but runblock, so that the controller has always been in the hands of the caller. Whether the block returns or not, the program will return to the caller: |
I like this proposal, but I think
There is a discussion about |
If we use |
No change in consensus, so declined. |
Go Programming Experience
Intermediate
Other Languages Experience
python,js,rust,java,c++
Related Idea
Has this idea, or one like it, been proposed before?
no
Does this affect error handling?
yes,I wish to introduce a new mechanism aimed at enhancing exception handling and improving coding practices.
Is this about generics?
no
Proposal
A new feature needs to be introduced: block statement, like this: block check_error(err error) {... } This is very similar to a function, but the block statement does not need to define a return value (it can return any value without limit), and is modified with the keyword
block
.block
defines the block statementrunblock
executes block statement(orgoto
)Here's an example:
before:
after:
demo1
demo2
The return value of the block will be strictly limited
Not only error handling, it can also be used in other places
In this way, the cumbersome
if err !=nil{}
can be placed in a commonly defined code block. When this judgment is needed, you only need to call this code block (very similar to calling a function).I believe that even without this feature, many developers have developed a function similar to
check_error
forif err !=nil{}
.However, the limitation of this function is that it cannot return to the outside of the function when it needs to return immediately, but only returns to the caller.
But this proposal can solve this problem. The usage is similar to that of a function. It is executed as a code block at the place where it is called. The most important thing is that the return statement will also be parsed as a return at the place where it is called.
And this proposal is different from
switch/case
: the program is always executed from top to bottom. Even in the place where the code block is called, it is similar to a function, and it does not avoid errors, but checks errors immediately. I know thatswitch/case
is a good proposal, but when I see the code ofswitch/case
, I always feel liketyr/catch
. It is not go.The
runblock
andblock
keywords can be replaced with more appropriateLanguage Spec Changes
Informal Change
This way, many repetitive error handling codes can be solved with one function, and I believe that many other places can also use this new feature of runblock.
I think this is a good proposal, because I did not deliberately solve the problem of
if err != nil {
, but added more expressive features to Go, and the new features can be the answer to the problem ofif err != nil {
I believe that the new feature of code blocks can be used in more places
Is this change backward compatible?
yes
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
No response
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
No response
Prototype
No response
The text was updated successfully, but these errors were encountered: