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

Iterable as List and Iterable.asList() have different semantics

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.5
    • 2.4.12
    • groovy-jdk
    • None

    Description

      For an Iterable `foo` which is not also a collection, `foo.asList()` and `foo as List` are not equivalent. The latter goes through the asType(Object, Class) path and ultimately returns a proxy. This is unexpected, and can result in some inconsistencies. A simple example:

      class IterableWrapper implements Iterable {
          Iterable delegate
      
          Iterator iterator() {
              delegate.iterator()
          }
      }
      
      def itw = new IterableWrapper(delegate: [1,2,3])
      def itwAsList = itw.asList()
      def itwAsTypeList = itw as List
      assert itwAsList == itwAsTypeList
      assert itwAsList[0] == itwAsTypeList[0]
      

      The first assertion passes, but the second fails with:

      groovy.lang.MissingMethodException: No signature of method: IterableWrapper.get() is applicable for argument types: (java.lang.Integer) values: [0]
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              shils Shil Sinha
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: