groovy

Array equality is not treated same as List equality

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-6
  • Fix Version/s: 1.1-rc-1
  • Component/s: groovy-jdk
  • Labels:
    None
  • Number of attachments :
    0

Description

There is no good reason why array equality should work differently from list equality:

groovy> def list1 = [1,2,3,4]
groovy> def list2 = [1,2,3,4]
groovy> assert list1 == list2
groovy> int[] arr1 = [1,2,3,4]
groovy> int[] arr2 = [1,2,3,4]
groovy> assert arr1 == arr2

Exception thrown: java.lang.AssertionError: Expression: (arr1 == arr2). Values: arr1 = [I@bc608f, arr2 = [I@677b56

Equality for lists compares the values, equality for arrays does identity. This seems very counter-intuitive, especially if you are writing code that does not mind whether it gets arrays or lists, and is not "Groovy".

Activity

Hide
Paul King added a comment -

I have a patch for this which works for Object types but primitive arrays are stored by Groovy in their primitive form and I need to make the patch work with that before submitting.

Show
Paul King added a comment - I have a patch for this which works for Object types but primitive arrays are stored by Groovy in their primitive form and I need to make the patch work with that before submitting.
Hide
Paul King added a comment -

I have applied a fix for single (and I think multi-level non-primitive) arrays, e.g. Integer[] and int[] but not nested primitive arrays, e.g. int[][]

Show
Paul King added a comment - I have applied a fix for single (and I think multi-level non-primitive) arrays, e.g. Integer[] and int[] but not nested primitive arrays, e.g. int[][]
Hide
Paul King added a comment -

See ArrayTest.groovy for what is currently covered, e.g. [[1,2] as Integer[]] == [[1,2]] as Object[]

Show
Paul King added a comment - See ArrayTest.groovy for what is currently covered, e.g. [[1,2] as Integer[]] == [[1,2]] as Object[]
Hide
Paul King added a comment -

Now working for multi-dimensional arrays, e.g.:
assert [[[5,6],[7,8]]] as int[][][] == [[[5,6],[7,8]]] as Long[][][]

Show
Paul King added a comment - Now working for multi-dimensional arrays, e.g.: assert [[[5,6],[7,8]]] as int[][][] == [[[5,6],[7,8]]] as Long[][][]
Hide
Paul King added a comment -

No further feedback, assuming fixed

Show
Paul King added a comment - No further feedback, assuming fixed

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: