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

Compile error when using generic type with lower bound

    XMLWordPrintableJSON

Details

    Description

      I'm trying to implement chained generics. It works as Java code but fails to compile by groovyc:

      Error:(38, 9) Groovyc: [Static type checking] - Cannot call <C extends com.github.ykiselev.ChainTest.A<? super O, ?>> com.github.ykiselev.ChainTest$A <? super O -> java.lang.Object, ?>#andThen(C) with arguments [com.github.ykiselev.ChainTest$A <Double, String>]
      

      Code (JUnit4 unit test)

      package com.github.ykiselev
      
      import groovy.transform.TypeChecked
      import org.junit.Test
      
      @TypeChecked
      class ChainTest {
      
          static class A<I, O> {
      
              def <C extends A<? super O, ?>> C andThen(C next) {
                  return next
              }
      
          }
      
          static <I, O, C extends A<? super O, ?>> C chain(A<I, O> self, C next) {
              self.andThen(next)
              return next
          }
      
          @Test
          void "should chain"() {
              def a1 = new A<String, Integer>()
              def a2 = new A<Integer, Double>()
              def a3 = new A<Double, String>()
              def a4 = new A<String, Double>()
              def a5 = new A<Number, Object>()
      
              a1.andThen(a2) // ok
              a2.andThen(a3) // ok
              a3.andThen(a4) // ok
              a4.andThen(a5) // ok (even without "? super O")
      
              chain(a1, a2).andThen(a3) // ok
              chain(chain(chain(chain(a1, a2), a3), a4), a5) // ok
      
              a1.andThen(a2)
                      .andThen(a3) // static type checker error
                      .andThen(a4)
                      .andThen(a5)
          }
      }
      

      This would compile if I change bounded type parameter like this:

      def <C extends A<O, ?>> C andThen(C next) {
                  return next
      }
      

      but I need it bounded.

      Attachments

        Activity

          People

            emilles Eric Milles
            uze@yandex.ru Yuriy Kiselev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: