codeconsole opened a new pull request, #3337:
URL: https://github.com/apache/groovy/pull/3337

   [GROOVY-12823](https://issues.apache.org/jira/browse/GROOVY-12823)
   
   Under `@CompileStatic`, a method reference passed to a generic method for a 
type parameter that only the call's target type gives did not compile:
   
   ```groovy
   @CompileStatic
   class C {
       static <T> T id(T t) { t }
       static String test() {
           Function<String, String> f = id(String::trim)
           // [Static type checking] - Argument is a method reference, but 
parameter type 'T' is not a functional interface
           f.apply(' a ')
       }
   }
   ```
   
   Since GROOVY-12727, the type a call's value is assigned, returned or cast to 
gives a type parameter that the arguments leave open, when the argument is a 
lambda or closure: `id((String s) -> s.trim())` compiles and runs. 
`getFunctionalTypeArgument`, which finds the functional interface of a method 
reference passed for a type parameter, took it from the arguments only.
   
   When no argument is a witness for the type parameter, it now takes the 
call's target type through GROOVY-12727's 
`extractGenericsConnectionsFromCallTarget`, which keeps a target outside the 
type parameter's bounds out. A method reference or constructor reference is 
then compiled for that interface. A call whose value has no target, such as 
`id(this::m)` as a statement, is still a compile error.
   
   ## Tests
   
   `MethodReferenceTest.testNotFunctionalInterface4` fails without the change: 
method references, a constructor reference with an argument and one without, 
passed to `id` and assigned, returned and cast. `testNotFunctionalInterface3`, 
which pins the error for a call without a target, still passes. The full 
`:test` suite and the lint gate pass.
   
   Seen in 6.0.0 and master.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to