[ 
https://issues.apache.org/jira/browse/GROOVY-10699?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17570042#comment-17570042
 ] 

Eric Milles commented on GROOVY-10699:
--------------------------------------

It is this bit from {{StaticTypeCheckingSupport}} that gets connections from 
{{Function<T,T>}} vs. {{(Double x) -> x}}.  At this time, only the return type 
is inspected.  And in the case of a method call argument, closures/lambdas have 
not been visited yet so their return type is unknown.

{code:java}
    static void extractGenericsConnections(final Map<GenericsTypeName, 
GenericsType> connections, final ClassNode type, final ClassNode target) {
        ...
        } else if (type.equals(CLOSURE_TYPE) && isSAMType(target)) {
            // GROOVY-9974, GROOVY-10052: Lambda, Closure, Pointer or Reference 
for SAM-type receiver
            ClassNode returnType = 
StaticTypeCheckingVisitor.wrapTypeIfNecessary(GenericsUtils.parameterizeSAM(target).getV2());
            extractGenericsConnections(connections, type.getGenericsTypes(), 
new GenericsType[] {returnType.asGenericsType()});
{code}

Next, it is this bit in {{StaticTypeCheckingVisitor}} that was enhanced for 
GROOVY-10436 to check for declared parameter types.
{code:java}
    protected void inferClosureParameterTypes(final ClassNode receiver, final 
Expression arguments, final ClosureExpression expression, final Parameter 
target, final MethodNode method) {
                        ...
                        extractGenericsConnections(gc, 
wrapTypeIfNecessary(getType(argument)), pType);
                        // GROOVY-10436: extract generics connections from 
closure parameter declaration(s)
                        if (argument == expression || (argument instanceof 
ClosureExpression && isSAMType(pType))) {
                            Parameter[] q = 
getParametersSafe((ClosureExpression) argument);
                            ClassNode[] r = extractTypesFromParameters(q); // 
maybe typed
                            ClassNode[] s = 
GenericsUtils.parameterizeSAM(pType).getV1();
                            for (int j = 0; j < r.length && j < s.length; j += 
1)
                                if (!q[j].isDynamicTyped()) 
extractGenericsConnections(gc, r[j], s[j]);
                        }
{code}

It is coming up empty even though it gets [Double] for parameter types (aka 
{{r}}).

> 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
>            Assignee: Eric Milles
>            Priority: Major
>
> 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 (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
>   }
> }
> {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)

Reply via email to