<div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><blockquote>
</blockquote>
<p>Unfortunately, Record Patterns in enhanced <font face="monospace">for</font> loops have been removed by <a href="https://openjdk.org/jeps/440" target="_blank">JEP 440</a> in Java 21. With
enabled preview features, you were able to write the following in
Java 20:</p>
<blockquote>
<pre>String[] strings = getSomeStrings();
for (ListIndex(int index, String element) : enumerate(strings)) {
System.out.println(index + ": " + element);
}
</pre>
</blockquote>
</div>
</blockquote></div><div>To be fair it looks ugly, especially the fact that you need to specify exact types. I would expect use of "var", at least for the element type.</div><div>Technically it could be implemented easily as an extension for streams-api, no changes in the language required.</div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">Stream.of(strings).forEachIndexed((index, element) -> {//BiFunction<br> System.out.println(index + ": " + element);<br>});</span><br></div><div><br></div><div>Or even make stream modifier to have allow proper chaining:</div><div><div><span style="font-family:monospace">Stream.of(strings)</span></div><div><span style="font-family:monospace"> .indexed()</span></div><div><span style="font-family:monospace"> .filter(i -> i</span><span style="font-family:monospace">.</span><span style="font-family:monospace">index() % 3 == 0 && !i.element().isEmpty())</span></div><div><span style="font-family:monospace"> .forEach(i -> {</span><br><span style="font-family:monospace"> System.out.println(i.index() + ": " + i.element());<br>});</span><br></div><div>This thing could also be <span style="font-family:monospace">parallelStream()</span></div></div><div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">WBR, Anatoly.</div></div></div>