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

STC: Closure implementation of Java @FunctionalInterface loses type information

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 4.0.5
    • 4.0.6
    • Static Type Checker
    • None

    Description

      The following fails to compile with Groovy 4.0.5:

      import java.util.function.BiFunction
      
      @groovy.transform.CompileStatic
      class MapTransformEntries {
          private static <K, V1, V2> Map<K, V2> transformEntries(Map<K, V1> map, BiFunction<? super K, ? super V1, V2> transformer) {
              throw new UnsupportedOperationException("implementation not relevant");
          }
      
          {
              Map<String, ? extends File> outputFiles
              Map<String, Integer> outputFileSpecs = transformEntries(outputFiles, { key, value -> value.hashCode() }) 
          }
      }
      

      Producing result:

      1 compilation error:
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Map<? extends java.lang.Object, java.lang.Integer> to: java.util.Map<java.lang.String, java.lang.Integer>
       at line: 11, column: 48
      

      At first glance, this seems similar to GROOVY-10613 and some of its related issues.

      We can workaround by casting the above to Map<String, Integer>, but this was not necessary in Groovy 3.x.

      Thanks to otogami for creating the reproducer

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              DPUkyle Kyle Moore
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: