You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "ocs@ocs.cz" <oc...@ocs.cz> on 2021/08/29 14:57:05 UTC

just bumped into some indexing inconsistencies

Hi there,

are these inconsistencies intentional?

I guess even if not, they would not be worth fixing (if for no other reason, then to keep backward compatibility with legacy code which happens to rely on the behaviour), but still, might be worth to point them out, and perhaps warn the users in the documentation. Perhaps I am just blind, but I could not find anywhere documented even the foo[toobigindex]==null behaviour (neither with [], nor with getAt).

941 ocs /tmp> <q.groovy                           
def a=[1,2]
println "Existing negative indexes work consistently: '${a[0]}', '${a[-1]}. Non-existing do not:"
[1,100,7e6,-3].each { println "a[$it]=${a[it]}" }
942 ocs /tmp> /usr/local/groovy-3.0.8/bin/groovy q
Existing negative indexes work consistently: '1', '2. Non-existing do not:
a[1]=2
a[100]=null
a[7E+6]=null
Caught: java.lang.ArrayIndexOutOfBoundsException: Negative array index [-3] too large for array size 2


943 ocs /tmp> <q.groovy                           
def a='hi'
println "With strings, existing indexes work consistently: '${a[0]}', '${a[-1]}'. Non-existing do not, differ even from arrays:"
[1,100,7e6,-3].each { println "a[$it]=${a[it]}" }
944 ocs /tmp> /usr/local/groovy-3.0.8/bin/groovy q
With strings, existing indexes work consistently: 'h', 'i'. Non-existing do not, differ even from arrays:
a[1]=i
Caught: java.lang.StringIndexOutOfBoundsException: String index out of range: 101

All the best,
OC