【发布时间】:2017-02-14 14:19:19
【问题描述】:
我在 groovy 中有以下代码:
class Test {
List<Integer> yo(boolean x) {
List<Integer> res = x ? [] : [1, 2, 3]
}
}
在我将@CompileStatic 注释添加到类之前,它编译得很好。然后编译失败
Test.groovy: 5: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.List <? extends java.lang.Object> to: java.util.List <Integer>
@ line 5, column 27.
List<Integer> res = x ? [] : [1, 2, 3]
是否真的期望 Groovy 无法推断该空列表 [] 的泛型类型?
【问题讨论】:
-
[]有一个 iferred 类型List<Object>,从我的 PoV 来看这很好。与null相同 -
在这种特殊情况下,具有
List<Integer>类型的 LHS 我以某种方式期望[]属于同一类型...因为List<Integer> l = []确实有效... -
这看起来像 Groovy 错误。