You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "Milles, Eric (TR Technology) via dev" <de...@groovy.apache.org> on 2022/09/19 19:59:24 UTC

RE: [EXT] indexing inconsistecy: bug or intentional?

This has been reported under a few different guises. There are some other tickets for ranges and negative indexes.

https://issues.apache.org/jira/browse/GROOVY-6193
https://issues.apache.org/jira/browse/GROOVY-4652
https://issues.apache.org/jira/browse/GROOVY-1740
https://issues.apache.org/jira/browse/GROOVY-1286


-----Original Message-----
From: ocs@ocs.cz <oc...@ocs.cz> 
Sent: Monday, September 19, 2022 2:45 PM
To: Groovy_Developers <de...@groovy.apache.org>
Subject: [EXT] indexing inconsistecy: bug or intentional?

External Email: Use caution with links and attachments.

Hi there,

is this behaviour intentional? Seems to me it should either throw in all these cases, or in none of them, but as always, I might be missing something of importance:

===
Groovy Shell (4.0.5, JVM: 1.8.0_181)
Type ':help' or ':h' for help.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> [][0]
===> null
groovy:000> [][666]
===> null
groovy:000> [][1e9]
===> null
groovy:000> [][-1]
ERROR java.lang.ArrayIndexOutOfBoundsException:
Negative array index [-1] too large for array size 0 groovy:000> [][-3] ERROR java.lang.ArrayIndexOutOfBoundsException:
Negative array index [-3] too large for array size 0 groovy:000> ===

Thanks and all the best,
OC


Re: [EXT] indexing inconsistecy: bug or intentional?

Posted by Paul King <pa...@asert.com.au>.
Yes, GROOVY-6193 sort of captures the discrepancy. You can think of
the current behavior as the result of multiple features interacting.
A list can be thought of as a map from index to value and, like
numerous map implementations, returns null if given a key (index
value) not in the map.
Now, negative numbers aren't keys in the map but instead of returning
null are given a special meaning to indicate index from the right.
Then we have special behavior like ListWithDefault which has eager and
lazy variants. It might grow the list if given an index out of range
but you wouldn't want it to grow the list below the zero index.

It has been a low priority to fix since if you have code that is
impacted by this discrepancy, it can probably be made clearer by
avoiding the edge case in the first place. Never-the-less, if you
stumble upon the edge case accidentally when debugging some code, a
clearer error message might be more helpful.



On Tue, Sep 20, 2022 at 5:59 AM Milles, Eric (TR Technology) via dev
<de...@groovy.apache.org> wrote:
>
> This has been reported under a few different guises. There are some other tickets for ranges and negative indexes.
>
> https://issues.apache.org/jira/browse/GROOVY-6193
> https://issues.apache.org/jira/browse/GROOVY-4652
> https://issues.apache.org/jira/browse/GROOVY-1740
> https://issues.apache.org/jira/browse/GROOVY-1286
>
>
> -----Original Message-----
> From: ocs@ocs.cz <oc...@ocs.cz>
> Sent: Monday, September 19, 2022 2:45 PM
> To: Groovy_Developers <de...@groovy.apache.org>
> Subject: [EXT] indexing inconsistecy: bug or intentional?
>
> External Email: Use caution with links and attachments.
>
> Hi there,
>
> is this behaviour intentional? Seems to me it should either throw in all these cases, or in none of them, but as always, I might be missing something of importance:
>
> ===
> Groovy Shell (4.0.5, JVM: 1.8.0_181)
> Type ':help' or ':h' for help.
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> groovy:000> [][0]
> ===> null
> groovy:000> [][666]
> ===> null
> groovy:000> [][1e9]
> ===> null
> groovy:000> [][-1]
> ERROR java.lang.ArrayIndexOutOfBoundsException:
> Negative array index [-1] too large for array size 0 groovy:000> [][-3] ERROR java.lang.ArrayIndexOutOfBoundsException:
> Negative array index [-3] too large for array size 0 groovy:000> ===
>
> Thanks and all the best,
> OC
>