On 2012/9/5 Aston Motes <
ast...@gmail.com> wrote:
> It seems like there's a missing for-loop form in Go that makes it a pain to
> emulate while loops as in other languages when dealing with multiple return
> values. Specifically, I wanted something like this to work (inspired by the
> syntax for if-statements and the "comma ok" idiom):
>
> for x, done := returns_multiple(); !done {
>
> // do stuff
>
> }
>
> It seems like my two existing options for pulling this off are as follows:
>
> for {
>
> x, done := returns_multiple()
> if done {
>
> break
>
> }
> //do stuff
>
> }
>
> for x, done:= returns_multiple();!done;x, done = returns_multiple() {
>
> //do stuff
>
> }
You have forgotten:
var x T
action = func() (done bool) { x, done := returns_multiple(); return }
for !action() {
// do something...
}
If you don't like your multiple return value function, don't use a
multiple return value function.
> Both of those seem to add a lot of noise to something reasonably simple. I'd
> argue that the syntax of this new form intuitively suggests what would
> happen if you omitted the third section of the typical for-loop. And I don't
> think there are any other meanings of a two clause for-loop that would cause
> confusion for users or would break currently working Go code.
A language construct can be confusing even when it has a single
meaning. I don't understand how it can ever become natural to me that
something that looks like an initialisation statement (like in all
other 2-clause construct we have: switch, select, if...) is supposed
to be repeated.
Rémy.