You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/10/25 16:11:00 UTC
[jira] [Commented] (GROOVY-7789) ClosureParams type inference
compilation error when wrapping closure with object
[ https://issues.apache.org/jira/browse/GROOVY-7789?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17433849#comment-17433849 ]
Eric Milles commented on GROOVY-7789:
-------------------------------------
{{wrap}} returns a raw type {{A}} regardless of what "IN" can be determined to be. There is no actual type witness for "IN". One can be supplied like this: {{def a_of_list = this.<List>wrap \{ i -> i.size() \};}} -- I'm assuming {{def <IN> A<IN> wrap(...)}}
In the case where an explicit type argument is supplied to the method call, Groovy 2.5 does not properly propagate the closure param type to the variable expression "i" in "i.size()".
{code:groovy}
import groovy.transform.stc.*
class A<T> {
Closure c
A(@ClosureParams(value=FromString, options='T') Closure c) {
this.c = c
}
def call(T param) {
c.call(param)
}
}
def <U> A<U> wrap(@ClosureParams(value=FromString, options='U') Closure c) {
new A<U>(c)
}
@groovy.transform.CompileStatic
def test7789() {
def a_of_list = this.<List>wrap { i -> i.size(); } // STC error in Groovy 2.5: Cannot find matching method Object#size()
def x = a_of_list.call([])
}
{code}
> ClosureParams type inference compilation error when wrapping closure with object
> --------------------------------------------------------------------------------
>
> Key: GROOVY-7789
> URL: https://issues.apache.org/jira/browse/GROOVY-7789
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 2.4.6
> Reporter: Krzysztof BiaĆek
> Priority: Major
>
> The following code does not compile:
> {code}
> import groovy.transform.*
> import groovy.transform.stc.*
> @CompileStatic
> class Test {
> class A<IN> {
> Closure c
> A(@ClosureParams(value = FromString, options = 'IN') Closure c) {
> this.c = c
> }
> void call(IN param) {
> c.call(param)
> }
> }
> def <IN> A wrap(@ClosureParams(value = FromString, options = 'IN') Closure c) {
> new A<IN>(c)
> }
> def test() {
> (wrap { i -> i.size() }).call([])
> }
> }
> {code}
> Compilation error:
> {code}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> /tmp/groovy-2.4.6/test.groovy: 25: [Static type checking] - Cannot find matching method java.lang.Object#size(). Please check if the declared type is right and if the method exists.
> @ line 24, column 22.
> (wrap { i -> i.size() }).call([])
> {code}
> I'd expect 'i' being infered as java.util.List
> Explicit this.<List>wrap does not help neither
--
This message was sent by Atlassian Jira
(v8.3.4#803005)