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

Closure with type argument inference does not work as expected

    XMLWordPrintableJSON

    Details

      Description

      I have the following Groovy program.

      @groovy.transform.CompileStatic
      public class Main {
        public static void main(String[] args) {
          def closure = {
            new Bar<>(new Foo<Long>());
          }
          closure().f.m(new BigDecimal[0]) // this doesn't work
          // the following works
          // Bar<Foo<Long>> x = closure()
          // x.f.m(new BigDecimal[0]);
        }
      }
      
      class Foo<X> {
        void m(Object[] x) {}
      }
      
      
      class Bar<X> {
        X f;
      
        Bar(X f) {
          this.f = f;
        }
      }
      
      class Baz {}
      
      

      Actual Behavior

      The program does not compile, and I get the following error.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Main.groovy: 7: [Static type checking] - Cannot find matching method java.lang.Object#m(java.math.BigDecimal[]). Please check if the declared type is correct and if the method exists.
       @ line 7, column 5.
             closure().f.m(new BigDecimal[0]) 
             ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Affected Version

      This programs fails when compiled with the compiler from the master (commit: a8023b1b0d1d79800677ce2b4665ee3fb14688f4) and with 4.0.0-alpha-2.

        Attachments

          Activity

            People

            • Assignee:
              emilles Eric Milles
              Reporter:
              schaliasos Stefanos Chaliasos
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: