In the following code, @CompileStatic
does not work in combination with the method reference operator for Groovy methods collect
and find
, although it works with Java streams or the method pointer operator. Am I doing something wrong or is this not supported?
The code does not compile using Java 17 and Groovy 4.0.15.
import groovy.transform.CompileStatic
@CompileStatic
private void groovyFuncWithMethodPointer(int n) {
// works
def res = (1..n).collect(this.&calc)
.find(this.&isEven)
}
@CompileStatic
private void groovyFuncWithMethodReference(int n) {
// does not work
// org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
// ideaGroovyConsole.groovy: 15: The argument is a method reference, but the parameter type is not a functional interface
def res = (1..n).collect(this::calc)
.find(this::isEven)
}
@CompileStatic
private void streamFuncWithMethodReference(int n) {
// works
def res = (1..n).stream()
.map(this::calc)
.filter(this::isEven)
.findFirst()
}
int calc(int x){
return 2*x+1
}
boolean isEven(int x){
return x % 2 == 0
}