Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null.collect() returns empty list

Tags:

groovy

I was writing unit tests when I happened to find that in groovy the below is true

null.collect({ //Anything }) == []

I could not find the reason for this. What part of groovy is giving this behavior? I checked NullObject but that does not have this collect method. So how is this happening?

like image 519
Aseem Bansal Avatar asked Apr 28 '26 13:04

Aseem Bansal


1 Answers

In groovy null has the iterator() method which returns an empty iterator. Calling collect on null is the same as null.iterator().collect({/*whatever*/}) and so this will be [].

See the comment on this bug report.

like image 66
halex Avatar answered Apr 30 '26 14:04

halex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!