Groovy ranges and lists


Some days ago, a friend of mine sent this post. Some hours after seeing it, I found my own “wat!”

Let´s talk about Groovy.

Groovy have ranges.

groovy> def a = [1..5]
groovy> def b = [1,2,3,4,5]
groovy> a == b 

Result: false

Wat?¡¡¡

groovy> def a = [1..5]
groovy> def b = [1,2,3,4,5]
groovy> println a.class
groovy> println b.class 

class java.util.ArrayList
class java.util.ArrayList

Now a bit of light in the problem

groovy> def a = [1..5]
groovy> def b = [1,2,3,4,5]
groovy> println a[0].class
groovy> println b[0].class 

class groovy.lang.IntRange
class java.lang.Integer

So the question is that “a” is a list with an IntRange in it. In order to work you have to get rid of bracket, which is a bit weird for me.

groovy> def a = 1..5
groovy> def b = [1,2,3,4,5]
groovy> a == b 

Result: true

I came to this trying to use the range in a case clause.