groovy-notifications mailing list archives

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


Jochen Theodorou commented on GROOVY-9064:

[~emilles] I agree in that the situation is quite less than ideal for generics. I think we
probably have to rethink the concept here a bit... the basic concept is the following:

X y = z

where X is the declaration type of y and Z is the inferred typed if z. In Flowtyping we say
if Z is a more specific type and X, Z is to be used. Def is just an alias for Object in this

And it works well for if X is List and Z is LinkedList. But if X is List<Object> and
Z is List<Object  & Serializable>, then Z is no longer a more specific type than
X in the normal sense, it is now an incompatible type. If this is not supposed to lead to
an compilation error, we should not go by the inferred type, but for the declared type for
the generics part.

> 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