You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2022/07/21 13:08:00 UTC

[jira] [Created] (GROOVY-10699) Unable to infer type argument from the parameter type of a lambda

Thodoris Sotiropoulos created GROOVY-10699:
----------------------------------------------

             Summary: Unable to infer type argument from the parameter type of a lambda
                 Key: GROOVY-10699
                 URL: https://issues.apache.org/jira/browse/GROOVY-10699
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


I have the following program:

{code}
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
    D<String> z = new D<>(() -> "fda"); // type inference works
    B<String> y = new B<>((String e) -> {return;} ); // type inference doesn't work
  }
}
{code}

h3. Actual behavior

{code}
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
{code}

h3. 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).



--
This message was sent by Atlassian Jira
(v8.20.10#820010)