groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Milles (JIRA)" <>
Subject [jira] [Commented] (GROOVY-9064) STC: explicit declared variable type ignored in favor of assigned value type(s)
Date Mon, 08 Apr 2019 14:30:00 GMT


Eric Milles commented on GROOVY-9064:

I perfectly understand flow typing applying to dynamic variables ({{CharSequence}} replaced
with {{def}} below):
void meth() {
  def cs = 'foo'
  assert cs.endsWith('oo') // (1)
  cs = "bar$cs"
  cs.with{ // (2)
    assert values[0] == 'foo'
    assert strings[0] == 'bar'

However, we have been fighting with {{@CompileStatic}} errors for a couple years now when
we declare {{Map<String,Object> map = [a:1,b:'2']}} and flow typing infers {{map}} as
{{LinkedHashMap<String,Serializable>}}.  I've explicitly declared the variable's type
and it is completely ignored from the very first assignment.

I also understand altering the type when it has been tested by an {{instanceof}}, however
this inferenced type should be a scoped to the statement or block that is guarded by the {{instanceof}}

> STC: explicit declared variable type ignored in favor of assigned value type(s)
> -------------------------------------------------------------------------------
>                 Key: GROOVY-9064
>                 URL:
>             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

View raw message