[ https://issues.apache.org/jira/browse/GROOVY-10682?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17607815#comment-17607815 ]
ASF GitHub Bot commented on GROOVY-10682: ----------------------------------------- eric-milles commented on code in PR #1785: URL: https://github.com/apache/groovy/pull/1785#discussion_r976644046 ########## src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java: ########## @@ -2273,6 +2273,26 @@ public static <T> T[] eachWithIndex(T[] self, @ClosureParams(value=FromString.cl return self; } + /** + * Iterates through an int[], + * passing each int and the element's index (a counter starting at + * zero) to the given closure. + * <pre class="groovyTestCase"> + * int result = 0 + * [0, 1, 2].eachWithIndex{ x, index {@code ->} result += x * index } + * assert result == 5 + * </pre> + * + * @param self an int array + * @param closure a Closure to operate on each item + * @return the self Object + * @since 1.0 + */ + public static int[] eachWithIndex(int[] self, @ClosureParams(value=FromString.class, options="int,Integer") Closure closure) { Review Comment: I think "int" should be "Integer". The closure params are passed as objects, so there is no optimization like `Consumer` versus `IntConsumer`. > Provide eachWithIndex for primitive arrays > ------------------------------------------ > > Key: GROOVY-10682 > URL: https://issues.apache.org/jira/browse/GROOVY-10682 > Project: Groovy > Issue Type: Improvement > Components: groovy-jdk > Reporter: Eric Milles > Assignee: Eric Milles > Priority: Minor > > Consider the following: > {code:groovy} > @groovy.transform.TypeChecked > void test(int[] ints) { > ints.eachWithIndex { value, index -> > println "$index: ${value.doubleValue()}" > } > } > test(0,1,2,3,4,5) > {code} > Compiler reports "[Static type checking] - Cannot find matching method > java.lang.Object#doubleValue()" > {{eachWithIndex}} is only provided for reference types, so "value" is seen as > Object by the type checker. -- This message was sent by Atlassian Jira (v8.20.10#820010)