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

Type cast methods for Collection

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.4.0-beta-4
    • groovy-jdk
    • None
    • Patch

    Description

      This is mainly my fault, current Groovy master branch cannot compile this code in @TypeChecked class,
      because Iterable.tail() returns Collection.

      void test() {
          assert 2 == foo([1, 2].tail())
      }
      
      int foo(List<Integer> a) { 
      	a[0] 
      }
      

      There is List.tail(), which returns List, but this method is @Deprecated,
      and @Deprecated method is not called.

      So I created a patch, in which many List methods return List, and also for many other Collection classes.
      For example, now List.leftShift() returns List, this code can be compiled in @TypeChecked class.

      ([1, 2] << 3).each { println it }.collect { it + 1 }.subList(1, 2)
      

      The implementation of List.leftShift() is just this. Only type casts.

      public static <T> List<T> leftShift(List<T> self, T value) {
          return (List<T>) leftShift((Collection<T>) self, value);
      }
      

      The pull request is here.
      https://github.com/groovy/groovy-core/pull/487

      Attachments

        Activity

          People

            melix Cédric Champeau
            yukoba Yu Kobayashi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: