groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-9064) STC: explicit declared variable type ignored in favor of assigned value type(s)
Date Sun, 07 Apr 2019 07:59:00 GMT

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

Paul King commented on GROOVY-9064:
-----------------------------------

I think there is more going on here. If you look at the inferred type for {{rows}} it is:
{code}
java.util.List <Object[]> -> java.util.List <E extends java.lang.Object>
{code}
The redirect is being used which is giving us {{Object}} which is why the {{rows}} declaration
is passing compilation at all.

In general, you wouldn't want the declaration type to be used when normal flow typing comes
into play:
{code}
@groovy.transform.CompileStatic
void meth() {
  CharSequence cs = 'foo'
  assert cs.endsWith('oo') // (1)
  cs = "bar$cs"
  cs.with{ // (2)
    assert values[0] == 'foo'
    assert strings[0] == 'bar'
  }
}
{code}
Here, we want {{String}} and {{GString}} to be the inferred types at (1) and (2) not {{CharSequence}}.

> 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: Improvement
>            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)

Mime
View raw message