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

Unable to infer type argument from the parameter type of a lambda

    XMLWordPrintableJSON

Details

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

    Description

      I have the following program:

      import java.util.function.Function;
      import java.util.function.Consumer;
      import java.util.function.Supplier;
      
      
      class A<T> {
        A(C<T> x) {}
      }
      
      class C<T> {
        C(T x ) {}
      }
      
      class B<X> {
        B(Consumer<X> x) {}
      }
      
      class D<X> {
        D(Supplier<X> x) {}
      }
      
      
      class Test {
        void test() {
          A<String> x = new A<>(new C<>("str")); // type inference works (type inferred based on the constant "str")
          D<String> z = new D<>(() -> "fda"); // type inference works (type inferred based on the return type of the lambda)
          B<String> y = new B<>((String e) -> {return;} ); // type inference doesn't work
        }
      }
      

      Actual behavior

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 27: [Static type checking] - Expected type java.lang.Object for lambda parameter: e
       @ line 27, column 28.
             B<String> y = new B<>((String e) -> {return;} ); // type inference doesn't work
                                    ^
      
      1 error
      

      Expected behavior

      Compile successfully

      Tested against master (commit: 63bcab1bf13fb3811626fb1727c22e86528feb7f)

      *Notes*: As indicated in the accompanying test case, the compiler is unable to infer the type argument of a parameterized constructor call from the parameter of a lambda, although it works for any other case (including the return type of a lambda).

      Attachments

        Activity

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: