groovy-notifications mailing list archives

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


Mauro Molinari commented on GROOVY-9064:

Making the compilation fail at:
List<Object[]> rows = getSomeRows()

would make more sense IMHO. But if we assume that this doesn't fail because Groovy is smart
enough to trust you that the returned {{List}} is actually a {{List<Object[]>}} and
save you from typing:
List<Object[]> rows = (List<Object[]) getSomeRows()

then the requirement to add the cast anyway not to make the compiler happy at the assignment
place, but rather to make the compiler happy within the {{each}} closure body (to convince
it that {{rows}} contains {{Object[]}} instances), just because the {{rows}} declaration type
is indeed completely ignored, well this sounds counter intuitive to me.

> 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