[
https://issues.apache.org/jira/browse/GROOVY-9267?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16944576#comment-16944576
]
Eric Milles commented on GROOVY-9267:
-------------------------------------
This is a duplicate of GROOVY-8409. There is a flat-map that is keyed by the
type parameter name for resolving generics. You can work around this issue by
using a type parameter name that is not used by java.util.function.Function.
> Static compilation confuses generic type parameters
> ---------------------------------------------------
>
> Key: GROOVY-9267
> URL: https://issues.apache.org/jira/browse/GROOVY-9267
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.4.15, 2.5.8, 3.0.0-beta-3
> Reporter: Onni Koskinen
> Priority: Major
>
> Given the following snippet
> {code:java}
> import java.util.function.*
> @groovy.transform.CompileStatic
> class A {
> static <T> T f(Function<String, T> funcParam) {
> T result = funcParam.apply("hello")
> return result
> }
>
> static <TT> TT g(Function<String, TT> funcParam) {
> TT result = funcParam.apply("hello")
> return result
> }
> }
> def result1 = A.f({ String s -> s.length() })
> def result2 = A.g({ String s -> s.length() })
> assert result1.class == result2.class{code}
> one would assume the assertion to pass (with both results being of class
> Integer). Instead f's type parameter T is apparently confused with Function's
> type parameter of the same name and we get the following result:
> {code:java}
> Assertion failed:
> assert result1.class == result2.class
> | | | | |
> '5' | | 5 class java.lang.Integer
> | false
> class java.lang.String
> at ConsoleScript0.run(ConsoleScript0:19)
> {code}
> Originally encountered with Groovy 2.4.15 but also verified to be present on
> latest stable 2.5.8 as well as 3.0.0-beta-3.
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)