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

Generic types get mixed up

    XMLWordPrintableJSON

Details

    Description

      When having a generic function inside a generic class with a constraint the generic types somehow get mixed up.

      In the following example the variable v should have the type V. But the generated bytecode contains a cast to the constraint on the generic type of the class.

       

      import groovy.transform.CompileStatic
      import java.util.function.Function
      
      @CompileStatic
      class Test<R extends Integer> {
        def <V> V method(Function<Test, V> f) {
          /**
           * org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '' with class 'java.lang.String' to class 'java.lang.Integer'
           * 	at Test.traverseFindResult(ConsoleScript4:7)
           * 	at Test$traverseFindResult.call(Unknown Source)
           * 	at ConsoleScript4.run(ConsoleScript4:12)
           */
          def v = f.apply(this) // V v = f.apply(this) doesn't work as well, but directly returning without assignment works
          return v
        }
      }
      
      new Test().method(new Function<Test, String>() {
        @Override
        String apply(Test test) {
          ""
        }
      })
      

       

      Attachments

        Activity

          People

            emilles Eric Milles
            fesc Felix Scheinost
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 40m
                1h 40m