<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"P Holder" <pholder@gmail.com><br><b>To: </b>"amber-dev" <amber-dev@openjdk.org><br><b>Sent: </b>Tuesday, December 5, 2023 10:00:50 AM<br><b>Subject: </b>Auto indexing improved for() loops</blockquote><div><br></div><div>Hello,<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div>I'm participating in Advent of Code 2023 using Java. </div></div></blockquote><div><br></div><div>So am i, but no loops only streams :). [1]<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div> It reminds me how I frequently wish I could have an index in a modern for loop without having to resort to using the old/traditional for loop. Now that the JEP 456 Unnamed Variables & Patterns is progressing nicely I can see how it could be used implicitly to grant my wish.</div><br><div>Instead of writing:</div><br><div>final String[] strings = getSomeStrings();<br></div><div>int index = 0;</div><div>for (final String str : strings)</div><div>{</div><div> // use the str and the index<br></div><div> index++;</div><div>}</div><br><div>and risking the chance I may forget to place the index++ ... I would rather have something like:</div><br><div><div>final String[] strings = getSomeStrings();<br></div>for (int index, final String str : strings)<div>{</div><div> // use the str and the index<br></div>}<br><div>where the new part "int index" is optional, and the compiler could treat it like "int _" if not specified. Of course it could also be long, one assumes.<br></div><br><div>I do realize it's merely syntactic sugar and I do know how to write the code I need, but it does surprise me the number of times I end up writing old for loops simply because I could use the index, but otherwise know doing it the modern way is cleaner and more expressive, if only I had the index too.</div></div></div></blockquote><div><br></div><div>One advantage of the current design is that it makes the intent of the developer clear,<br data-mce-bogus="1"></div><div>if it's a simple loop, use the enhanced for loop, if it's more complex, then use the classical C for loop</div><div>(both IntelliJ and Eclipse knows how to go back and forth).<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>By introducing an index in the design, you are muddying the water because now you can have loop that does side effect on the data structure you are looping on,<br data-mce-bogus="1"></div><div>By example, I'm not sure i like the following code<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div> List<String> list = ...<br data-mce-bogus="1"></div><div> for(int index, String s : strings) {<br data-mce-bogus="1"></div><div> strings.set(index, ...);<br data-mce-bogus="1"></div><div> }<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Also, looping with an index on a List is a kind of dangerous if you do not know the implementation of the List,<br data-mce-bogus="1"></div><div>With the code above if 'strings' being a LinkedList<String>, the worst case complexity is O(n2). Again, not something we want people to write.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I think i would prefer to have to have an indexed stream more than indexed loop, the good news is that it seems something we can do using the gatherer API [2] and Valhalla (to avoid the cost of creating a a pair (index, element) for each element).<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>regards,<br data-mce-bogus="1"></div><div>RĂ©mi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>[1] <a href="https://github.com/forax/advent-of-code-2023">https://github.com/forax/advent-of-code-2023</a><br data-mce-bogus="1"></div><div>[2] <a href="https://openjdk.org/jeps/461">https://openjdk.org/jeps/461</a><br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>