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

Type inference does not work with GDK class enhancements

    XMLWordPrintableJSON

Details

    Description

      Consider the following code:

       

      @groovy.transform.TypeChecked
      public class T2 {
          def m(Object v) {
              if (v instanceof List) {
                  System.err.println v.get(1)
                  List x = (List)v;
                  System.err.println x[0]
                  //System.err.println v[0]           <--- Uncoment to get error
              } else {
                  System.err.println "Not a list"
              }
          }
      }
      
      T2 t = new T2();
      t.m(Arrays.asList("a", "b"));
      
      

      This code compiles. But when the marked line is uncommented, groovy produces an error:

      /tmp/g/ee/T.groovy: 9: [Static type checking] - Cannot find matching method java.lang.Object#getAt(int). Please check if the declared type is correct and if the method exists.
      @ line 9, column 32.
                    System.err.println v[0]

      Basic inference works OK, as the v.get(int) call compiles, but seems the GDK enhancements (List.getAt which interfaces with the [] operator) are not considered.

      Tested on Groovy Version: 3.0.8 JVM: 11.0.5 Vendor: Oracle Corporation OS: Linux

      Attachments

        Activity

          People

            emilles Eric Milles
            sdedic Svatopluk Dedic
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: