PROPOSAL: Enhanced for each loop iteration control
Reinier Zwitserloot
reinier at zwitserloot.com
Wed May 6 03:03:50 PDT 2009
The nicety of the original proposal was the added utility methods,
such as .first().
Getting the index of the loop? Don't care. Don't want. Or at least,
not often enough to make this a language change. This is true for
every method in the utility set, but add them together, and you have
something useful enough to consider adding to the language. I find
this hijacking of the #, or the 4-sub-statement for statement, both
far higher inpact resp. far more complicated, for far less value, than
the original 3-way colon-separated for proposal that was not even
shortlisted by Joe Darcy. Why are we talking about this again?
--Reinier Zwitserloot
On May 6, 2009, at 11:45, Bruce Chapman wrote:
>
>
>> I don't understand what you mean by "breaking on nested loops".
>> Support for nested loops is why I suggested "#foo" instead of just
>> "#"... the specification use of the loop variable indicates the
>> nesting level. For instance, you could do something like:
>>
>> for (Thing foo : whatever) {
>> for (Thing bar : whateverElse) {
>> doSomethingWith(foo, bar, #foo, #bar);
>> }
>> }
>>
>> Derek
>>
>>
>>
> I am not really excited about this proposal, but I just had this
> thought
> for a possible syntax.
>
> The idea is to hybridize the for-each and conventional for syntax, but
> you don't need the (stopping) expression from conventional syntax
> (foreach does that), but you could include the initializing portion
> and
> updating portion like this
>
> HybridForStatement
>
> for ( /VariableModifiers_opt Type Identifier /: Expression ; /
> ForInit_/opt/ / //; /ForUpdate_/opt/ / ) /Statement
>
> Then to do an indexed loop you could write
> /
>
> for (Thing foo : whatever; int i=0; i++) {
> for (Thing bar : whateverElse; int j=0; j++ ) {
> doSomethingWith(foo, bar, i, j);
> }
> }
>
> but it's not that much different than what you can do (irrespective
> of the exotic formatting) already
>
> int i=-1; for (Thing foo : whatever) { i++;
> int j=-1; for (Thing bar : whateverElse ) { j++;
> doSomethingWith(foo, bar, i, j);
> }
> }
>
>
> Bruce
>
>
More information about the coin-dev
mailing list