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 Sun, 07 Apr 2019 14:05:00 GMT


Mauro Molinari commented on GROOVY-9064:

Thanks for the pointer Paul, I now understand this better. Personally, from a fan of statically
typed languages, I find it a bit confusing and risky, but I understand the point when the
documentation says that flow typing was introduced to reduce the difference in semantics between
classic and static Groovy.

So, from this point of view, the behaviour of groovyc with the example provided in the steps
to repro is perfectly correct.

However, given the fact that {{List}} is often a shortcut for "list of I don't care of", rather
than "list of Objects" (so, something more like {{List<?>}} rather than {{List<Object>}}),
I think that in this specific case (which is something like the opposite of the example provided
by the documentation, {{List list = ['a','b','c']}}, where the declared type is less specific
than the inferred type), where the generic type is made more specific and explicit by the
declared type of {{rows}} variable, having such a behaviour is not only counter-intuitive,
but even questionable...

What do you think?

> 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