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: looping with deferred condition as do while #34896

Closed
gocs opened this issue Oct 14, 2019 · 3 comments
Closed

proposal: Go 2: looping with deferred condition as do while #34896

gocs opened this issue Oct 14, 2019 · 3 comments
Labels
Milestone

Comments

@gocs
Copy link

gocs commented Oct 14, 2019

for defer true {
}

This should work the same as:

do {
} while (true);

https://play.golang.org/p/mX8ZYqkygBL

for defer false {
    // runs once
}

for i := 0; defer i < 1; i++ {
    // runs once
    // i++ is also a deferred statement?
    // evaluate defer condition like LIFO
}

I was thinking of deferred conditions in general, but that is madness.

if defer false {
    // executes this block and neglects any else if
}

switch {
case defer true:
    // executes this block and neglects any else if
}
@gopherbot gopherbot added this to the Proposal milestone Oct 14, 2019
@jfesler
Copy link

jfesler commented Oct 14, 2019

This muddies the idea of when deferred functions are ran. Is it at the end of the function? Is it at the end of do/while? And I don't even know how to parse the intent on the if defer false or the switch mentioned above. Overloading "defer" like this would make code reviews painful.

If you really want your condition defined at the top of the loop, but executed at the end..

for ok := true; ok; ok = condition {
    // runs at least once
}

@ianlancetaylor ianlancetaylor changed the title Proposal: looping with deferred condition as do while proposal: Go 2: looping with deferred condition as do while Oct 14, 2019
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Oct 14, 2019
@griesemer
Copy link
Contributor

From your examples it is unclear what you're trying to achieve here. Also, there doesn't seem to be much support for it. For these reason this is a likely-decline. Leaving open for 4 weeks for comments.

@ianlancetaylor
Copy link
Contributor

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
Projects
None yet
Development

No branches or pull requests

5 participants