Paul King created GROOVY-12776:
----------------------------------

             Summary: STC: a constructor reference is not checked against the 
parameter types of a functional interface whose type parameters a generic call 
infers
                 Key: GROOVY-12776
                 URL: https://issues.apache.org/jira/browse/GROOVY-12776
             Project: Groovy
          Issue Type: Bug
            Reporter: Paul King


{code:groovy}
import java.util.function.Function

@groovy.transform.CompileStatic
class C {
    static <T, R> Function<T, R> toFunction(Function<T, R> fn) { fn }
    static main(args) {
        Function<Object, Object> f = toFunction(StringBuilder::new)
        println f.apply(new Object())
    }
}
{code}

This compiles with 4.0.33, 5.1.3, 6.0.0 and master, under {{@CompileStatic}} 
and {{@TypeChecked}}, and then fails:
{noformat}
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: 
java.lang.StringBuilder(Object)
{noformat}

{{StringBuilder}} has no constructor that takes an {{Object}}. javac rejects 
the equivalent: "cannot infer type-variable(s) T,R".

The method-reference form, {{Function<Object, Integer> f = 
toFunction(String::length)}}, is rejected. For a constructor reference, {{T}} 
is left to be inferred as {{Object}} without checking that a constructor 
accepts it.

*Expected:* a compile error unless a constructor of the class accepts the 
functional interface's parameter types, as {{Function<String, StringBuilder> f 
= toFunction(StringBuilder::new)}} does.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to