Skip to content
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: extend "continue" to jump to start of function #34144

Closed
jfcg opened this issue Sep 6, 2019 · 5 comments
Closed

proposal: Go 2: extend "continue" to jump to start of function #34144

jfcg opened this issue Sep 6, 2019 · 5 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Milestone

Comments

@jfcg
Copy link

jfcg commented Sep 6, 2019

continue statement begins the next iteration of the innermost "for" loop at its post statement. The "for" loop must be within the same function. If there is a label, it must be that of an enclosing "for" statement, and that is the one whose execution advances.

In addition to the above officially specified definition of continue keyword, I am proposing to extend its definition to also include the containing function body as the outer-most layer. That means:

  • "continue" goes to the start of the containing function if there is no enclosing for loop
  • "continue functionName" goes to the start of the containing function

I occasionally find myself using for {} or labels/gotos for an entire function, so I thought this would be a harmless and useful addition to Go. What do you think?

Note: When examining this, I realized we can create labels inside functions with the same name. So you can have a label main: inside main(). So I think it would be better to forbid such label names inside a function if this proposal is accepted.

Note 2: Since we have return, we don't need to change anything about "break".

@gopherbot gopherbot added this to the Proposal milestone Sep 6, 2019
@ianlancetaylor ianlancetaylor changed the title Proposal: Go 2: extend "continue" to jump to start of function proposal: Go 2: extend "continue" to jump to start of function Sep 6, 2019
@ianlancetaylor ianlancetaylor added v2 An incompatible library change LanguageChange Suggested changes to the Go language labels Sep 6, 2019
@ianlancetaylor
Copy link
Member

Within a loop, continue has a vaguely sensible meaning: continue with the next iteration of the loop. I can't see any way that continue implies that we should jump to start of the current function. A function is not a loop.

It also doesn't seem like something that comes up all that often. In https://blog.golang.org/go2-here-we-come Robert outlined three criteria for language changes. I'm not sure this meets criteria 1: address an important issue for many people.

@beoran
Copy link

beoran commented Sep 10, 2019

As you noted, this can already be done using goto and a label. However, I would say that it's better in most cases to refactor the function and split the loop body into a separate function, that returns whether or not a continue is needed as a boolean.

@mdaliyan
Copy link

mdaliyan commented Oct 1, 2019

"continue functionName" makes no sense. What you say is some kind of "repeat functionName" that makes no sense either. You can just run your function recursively or use labels.

@bradfitz
Copy link
Contributor

This doesn't seem to have much, if any, support, so this looks like it'll be a likely decline.

Leaving open for four more weeks for final comments, though.

@ianlancetaylor
Copy link
Member

No final comments.

@golang golang locked and limited conversation to collaborators Nov 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants
@bradfitz @beoran @jfcg @mdaliyan @ianlancetaylor @gopherbot and others