Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.4.15, 3.0.4
-
None
Description
When having a generic function inside a generic class with a constraint the generic types somehow get mixed up.
In the following example the variable v should have the type V. But the generated bytecode contains a cast to the constraint on the generic type of the class.
import groovy.transform.CompileStatic import java.util.function.Function @CompileStatic class Test<R extends Integer> { def <V> V method(Function<Test, V> f) { /** * org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '' with class 'java.lang.String' to class 'java.lang.Integer' * at Test.traverseFindResult(ConsoleScript4:7) * at Test$traverseFindResult.call(Unknown Source) * at ConsoleScript4.run(ConsoleScript4:12) */ def v = f.apply(this) // V v = f.apply(this) doesn't work as well, but directly returning without assignment works return v } } new Test().method(new Function<Test, String>() { @Override String apply(Test test) { "" } })