Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6989

.each{..} Closure With Parameter Typed As Object[] Makes Arguments Object[Object[]]

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.3, 2.4.0-beta-1, 2.3.4, 2.3.5, 2.3.6
    • 2.4.0-beta-4
    • None
    • Windows 7 x64 SP1 on JVM 1.7.0_17

    Description

      When iterating over a List of Object[] in a .each closure and typing the parameter as Object[], the arguments in each iteration become an Object[] of Object[]. This does not happen if the parameter is def.

      Object[] arr = new Object[1]
      arr[0] = 'test'
      List list = new ArrayList()
      list.add(arr)
      
      list.each { def obj ->
          assert obj[0] == 'test'
      }
      
      list.each { Object[] obj ->
          assert obj[0] == 'test' //fails. obj[0] is Object[]{'test'} instead of just 'test'
      }
      

      This does not happen if the parameter is typed as String[]:

      String[] arr = new String[1]
      arr[0] = 'test'
      List list = new ArrayList()
      list.add(arr)
      
      list.each { def obj ->
          assert obj[0] == 'test'
      }
      
      list.each { String[] obj ->
          assert obj[0] == 'test' // passes
      }
      

      I have included a more thorough test case that demonstrates this behavior. Note that this may also apply to .collect or various other methods.

      Attachments

        1. test.groovy
          2 kB
          Alex Beardsley

        Issue Links

          Activity

            People

              blackdrag Jochen Theodorou
              nalandial Alex Beardsley
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: