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 2019/04/03 15:48:00 UTC

[jira] [Commented] (GROOVY-9064) STC: explicit declared variable type ignored in favor of assigned value type(s)

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

Eric Milles commented on GROOVY-9064:
-------------------------------------

To maintain the declared type, I propose the following change in {{StaticTypeCheckingVisitor.visitBinaryExpression}}:

{code:java}
            if (!isEmptyDeclaration && isAssignment(op)) {
                ...
                // GRECLIPSE add
                if (!(leftExpression instanceof VariableExpression) || ((VariableExpression) leftExpression).isDynamicTyped())
                // GRECLIPSE end
                storeType(leftExpression, resultType);
{code}

It is possible another condition should be added for assignment to property expressions as well, like {{foo.bar = value}}.  In any case, this is a significant enough STC change that I believe it should not be made in Groovy 2.5, but can be made in Groovy 3+.

> STC: explicit declared variable type ignored in favor of assigned value type(s)
> -------------------------------------------------------------------------------
>
>                 Key: GROOVY-9064
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9064
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Eric Milles
>            Priority: Major
>
> Follow up to GROOVY-9058.  Consider the following:
> {code:groovy}
> List getSomeRows() { ... }
> @groovy.transform.CompileStatic
> void meth() {
>   List<Object[]> rows = getSomeRows()
>   rows.each { row ->
>     def col = row[0]
>   }
> }
> {code}
> The inferred type of {{rows}} is {{List}} and not {{List<Object[]>}} even though the assignment cleared type checking.  This causes the inferred type of {{row}} to be {{Object}} instead of {{Object[]}}.
> Similarly, {{List<String> list = []}} infers as {{ArrayList<String>}} instead of the explicit declared type, and {{Map<String, ?> map = [:]}} infers to {{LinkedHasMap<...>}} instead of the declared type.  In general, I think as long as the assignment is compatible, the variable should retain its explicitly declared type regardless of assignments.



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