1

I am trying to understand the behaviour in the following code examples

groovy:000> [][0] ===> null groovy:000> [][0..0] ERROR java.lang.IndexOutOfBoundsException: toIndex = 1 at groovysh_evaluate.run (groovysh_evaluate:2) ... groovy:000> 

Why does the first code segment return null, whereas the second throw an IndexOutofBoundsException. My expectation was for this to be consistent with the outcome of the first code fragment.

I am using the groovy compiler 2.1.4

Thanks.

1 Answer 1

4

[][0..0] must be calling List.subList( from, to ) which throws an AIOOBE. the [][0] calls a groovy's getAt(ix) method wich is safe

btw [].first() results also in NoSuchElementException

I think, that the behavior is quite consistent

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.