<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <font size="4" face="monospace">The issue you raise -- that the
      for-each loop does not give you access to the index, and that to
      get it you have to fall all the way back to an old-style iterator
      loop -- is a valid concern (in fact, I raised this as a comment
      during JSR 201.)  <br>
      <br>
    </font><font size="4" face="monospace">The syntax that you propose
      looks pretty "nailed on", though; there are contexts in the
      language where a single variable declaration is used in
      conjunction with some other syntax construct, including the
      foreach loop:<br>
      <br>
          for (VariableDecl : Expression)<br>
      <br>
      and contexts where there are multiple variable declarations
      separated by commas (such as method declaration), but no context
      where there are exactly two *and then some weird stuff*.  There is
      no existing model here to appeal to about what<br>
      <br>
          for (VariableDecl, VariableDecl : Expression)<br>
      <br>
      would mean, which adds cognitive load for users (among other
      things.)  <br>
      <br>
      A more grounded approach would be something like:<br>
      <br>
          interface ListIndex<T> {<br>
              int index();<br>
              T element();<br>
          }<br>
      <br>
      and allow a ListIndex<T> to be used as the induction
      variable for an iteration:<br>
      <br>
          for (ListIndex<String> i : strings) { <br>
              ... i.element()  ... i.index()<br>
          }<br>
      <br>
      There are two "obvious" objections; one is that it is more wordy,
      and the other is "mumble mumble but performance".  But the latter
      goes away with Valhalla, so let's not speak of it again.  <br>
      <br>
      It seems something we could pursue at some point in the future,
      but probably after Valhalla.<br>
      <br>
      <br>
    </font><br>
    <div class="moz-cite-prefix">On 12/5/2023 4:00 AM, P Holder wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAG7BDeZtKONKfhxEBT8y6GRXu_L7XPA60ZpZJ9Qhwkbs6g_vag@mail.gmail.com">
      
      <div dir="ltr">
        <div>I'm participating in Advent of Code 2023 using Java.  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>
        <div><br>
        </div>
        <div>Instead of writing:</div>
        <div><br>
        </div>
        <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>
        <div><br>
        </div>
        <div>and risking the chance I may forget to place the index++
          ... I would rather have something like:</div>
        <div><br>
        </div>
        <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>
          }
          <div><br>
          </div>
          <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>
          <div><br>
          </div>
          <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.<br>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
  </body>
</html>