[ 
https://issues.apache.org/jira/browse/GROOVY-8029?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15787143#comment-15787143
 ] 

John Wagenleitner commented on GROOVY-8029:
-------------------------------------------

I think there might be two issues here.

First, the STC should probably fail to compile without having to do a second 
pass on the expression.  The second pass is caused by the elvis operator 
evaluating the {{collectMany}} call both for the boolean and then for true 
expression and the inferred type is not set in the node metadata for closures 
until after the first evaluation.  The parameter for {{collectMany}} is 
{{Closure<Collection<? extends T>>}} and the inferred type of the argument is 
{{Closure<List>}}.

Note the following will fail with the same message as when the elvis is used 
(see GROOVY-6219):

{code}
@groovy.transform.CompileStatic
List<String> getStrings(List<Object> l) {
    def proj =  { [it.toString()] }
    l.collectMany(proj)
}
{code}

{code}
1 compilation error:

[Static type checking] - Cannot call <T,E> java.util.List 
<Object>#collectMany(groovy.lang.Closure <java.util.Collection>) with arguments 
[groovy.lang.Closure <java.util.List>] 
 at line: 4, column: 5
{code}

Second, the {{collectMany}} parameter should probably be changed to {{Closure<? 
extends Collection<T>>}}.

> Static compilation fails when using collectMany and elvis operator
> ------------------------------------------------------------------
>
>                 Key: GROOVY-8029
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8029
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 2.4.7
>            Reporter: Dmitry Bobrov
>            Priority: Minor
>
> The following code, when used with {{\@CompileStatic}}, produces {{Groovyc: 
> [Static type checking] - Cannot call <T,E> java.util.List 
> <Object>#collectMany(groovy.lang.Closure <java.util.Collection>) with 
> arguments [groovy.lang.Closure <java.util.List>]}}
> {code}
> List<String> getStrings(List<Object> l) {
>     l.collectMany { [it.toString()] } ?: ([] as List<String>)
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to