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

Eric Milles commented on GROOVY-9058:
-------------------------------------

{{left}} is {{List<Object[]>}}, {{right}} is {{List}}, {{op}} is {{ASSIGN}}, 
and {{expr}} is a {{DeclarationExpression}}.  *In the case of a declaration 
expression with an explicit type (aka {{List<Object[]> foo = new 
Foo().bar()}}), should the explicit type always be returned here?*

{code:java}
    protected ClassNode getResultType(ClassNode left, int op, ClassNode right, 
BinaryExpression expr) {
        ClassNode leftRedirect = left.redirect();
        ClassNode rightRedirect = right.redirect();

        Expression leftExpression = expr.getLeftExpression();
        Expression rightExpression = expr.getRightExpression();
        if (op == ASSIGN || op == ASSIGNMENT_OPERATOR) {
            if (leftRedirect.isArray() && 
implementsInterfaceOrIsSubclassOf(rightRedirect, Collection_TYPE)) return 
leftRedirect;
            if (leftRedirect.implementsInterface(Collection_TYPE) && 
rightRedirect.implementsInterface(Collection_TYPE)) {
                // because of type inferrence, we must perform an additional 
check if the right expression
                // is an empty list expression ([]). In that case and only in 
that case, the inferred type
                // will be wrong, so we will prefer the left type
                if (rightExpression instanceof ListExpression) {
                    List<Expression> list = ((ListExpression) 
rightExpression).getExpressions();
                    if (list.isEmpty()) return left;
                }
                return right; // returns "java.util.List" from here
            }
{code}

> each parameter type not correctly inferenced
> --------------------------------------------
>
>                 Key: GROOVY-9058
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9058
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.5.6
>            Reporter: Mauro Molinari
>            Priority: Major
>
> Consider this Java class:
> {code:java}
> package test51;
> import java.util.List;
> public class Foo {
>     public List<Object[]> bar() { return null; }
> }{code}
>  and this Groovy class:
> {code:java}
> package test51
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test51 {
>     protected void foo() {
>         List<Object[]> foo = new Foo().bar()
>         foo.each { row ->
>             def o = row[0]
>         }
>     }
>     
>     List bar() {
>     }
> }{code}
> This produces a compiler error because {{row}} is resolved as {{Object}} 
> rather than {{Object[]}}.
> A workaround is to declare {{row}} as {{Object[] row}} in the closure 
> parameter list.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to