groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mauro Molinari (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 13:22:00 GMT

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

Mauro Molinari commented on GROOVY-9064:
----------------------------------------

Hi Paul,

are you sure? I mean, in a static context I expect {{cs}} to be inferred as {{CharSequence}}
at both (1) and (2), requiring a cast otherwise. After all, Java behaves in this way. If we
are talking about a dynamic context, the story may be different, of course...

We might say that Groovy is "smart enough" to automatically convert {{CharSequence}} to {{String}}
at (1) and to {{GString}}{{}} at (2), without requiring a cast, but this sounds to me something
more like a "auto(un)boxing" feature, not an actual type change for {{cs}}...

Just IMHO of course.

> 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