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

Comments

@gocs
Copy link

@gocs 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
@gopherbot gopherbot added the Proposal label Oct 14, 2019
@jfesler

This comment has been minimized.

Copy link

@jfesler 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
@griesemer

This comment has been minimized.

Copy link
Contributor

@griesemer griesemer commented Oct 22, 2019

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

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 19, 2019

No final comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.