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 2020/07/13 16:08:00 UTC

[jira] [Comment Edited] (GROOVY-7848) Closure generic parameters ignored and return types missing from collections methods

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

Eric Milles edited comment on GROOVY-7848 at 7/13/20, 4:07 PM:
---------------------------------------------------------------

You can rewrite the "bar" case like this for no STC errors:
{code:groovy}
@groovy.transform.CompileStatic
def bar() {
  List<List<Integer>> pairs = [[1,3], [1,2]].transpose()
  pairs.inject(true) { flag, pair -> flag && pair[0] == pair[1] }
}
{code}



was (Author: emilles):
You can rewrite the "bar" case like this for no STC errors:
{code:groovy}
@groovy.transform.CompileStatic
def bar() {
  List<List<Integer>> result = [[1,3], [1,2]].transpose()
  result.inject(true) { flag, pair -> flag && pair[0] == pair[1] }
}
{code}


> Closure generic parameters ignored and return types missing from collections methods
> ------------------------------------------------------------------------------------
>
>                 Key: GROOVY-7848
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7848
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Dario Bertini
>            Assignee: Eric Milles
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> The theme of this ticket is type failures even specifying the correct input parameter type in a Closure, specifically these 2 methods fail to compile with CompileStatic/TypeChecked:
> {code}
> @CompileStatic
> static List foo(){
>     [[1,2], [3,4]].collect{List<Integer> pair -> pair[0]+pair[1]}
> }
> @CompileStatic
> static bar(){
>     [[1, 3], [1, 2]].transpose().inject(true){acc, List pair -> acc && pair[0] == pair[1]}
> }
> {code}
> The first is due to the generic parameter Integer apparently being ignored.
> bq. Error:(15, 48) Groovyc: [Static type checking] - Cannot find matching method java.lang.Object#plus(java.lang.Object). Please check if the declared type is right and if the method exists.
> Ignoring it is just fine when compiling in Dynamic mode, but makes it apparently impossible to write a similar Type Safe method
> Apparently, this part of the language is as yet undocumented:
> http://docs.groovy-lang.org/latest/html/documentation/index.html#generics
> The second method instead, has a similar failure to locate Object#getAt(int), but the actual error is this one:
> bq. Error:(20, 50) Groovyc: Expected parameter of type java.lang.Object but got java.util.List
> Which is imho a bit unhelpful: I'm obviously passing it a List, at runtime... and the the Closure parameter type is List, given that I would expect the error to be the other way around: expected parameter of type List, but got Object
> But the problem is not actually with Closure types, inference or the like... I suspect it might simply be an imprecise type specified for transpose() result: it should be List<List>, but is instead List (once I understood this I simply created an intermedia variable with the explicit type as a workaround)



--
This message was sent by Atlassian Jira
(v8.3.4#803005)