You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/07/07 15:08:00 UTC
[jira] [Created] (GROOVY-10683) Consider enhancing for loop to provide access to loop index or iterator
Eric Milles created GROOVY-10683:
------------------------------------
Summary: Consider enhancing for loop to provide access to loop index or iterator
Key: GROOVY-10683
URL: https://issues.apache.org/jira/browse/GROOVY-10683
Project: Groovy
Issue Type: New Feature
Components: parser-antlr4
Reporter: Eric Milles
When iterating over an array or iterable, there is choice between traditional counting loop for index access, declaring/managing an external variable to leverage for-each loop, or switching to {{eachWithIndex}} which requires quite a bit more runtime investment.
{code:groovy}
for (int i = 0, n = array.length; i < n; i += 1) {
def val = array[i]
}
int idx = 0; for (val in array) {
idx += 1;
}
// idiomatic and takes care of all the management, but lacks STC support for primitive arrays and adds the cost of method invocation for each iteration
array.eachWithIndex { val, idx ->
}
{code}
Please consider the possibility of {{for}} loop enhancements of [Gosu|https://gosu-lang.github.io/docs.html], which provides a direct syntax for index or iterator:
{code}
for (val in array index idx) {
}
for (value in iterable iterator iter) {
}
{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)