Generics 带有泛型列表的Groovy CompileStatic

Generics 带有泛型列表的Groovy CompileStatic,generics,groovy,Generics,Groovy,我在groovy中有以下代码: class Test { List<Integer> yo(boolean x) { List<Integer> res = x ? [] : [1, 2, 3] } } Groovy真的不能推断空列表的泛型类型吗?我建议您不要使用泛型。对我来说,它编译和运行良好,如下所示: List res = x ? [] : [1, 2, 3] 但如果您仍然非常需要泛型,请尝试以下方法: List<Integer> r

我在groovy中有以下代码:

class Test {
  List<Integer> yo(boolean x) {
    List<Integer> res = x ? [] : [1, 2, 3]
  }
}

Groovy真的不能推断空列表的泛型类型吗?

我建议您不要使用泛型。对我来说,它编译和运行良好,如下所示:

List res = x ? [] : [1, 2, 3]
但如果您仍然非常需要泛型,请尝试以下方法:

List<Integer> res = x ? [] as List<Integer> : [1, 2, 3]
List res=x?[]作为列表:[1、2、3]

我建议您根本不要使用泛型。对我来说,它编译和运行良好,如下所示:

List res = x ? [] : [1, 2, 3]
但如果您仍然非常需要泛型,请尝试以下方法:

List<Integer> res = x ? [] as List<Integer> : [1, 2, 3]
List res=x?[]作为列表:[1、2、3]

[]
有一个红色的类型
列表
,这在我的PoV中很好。与
null
相同,在这种特殊情况下,具有类型为
List
的LHS,我不知怎的期望
[]
为相同类型。。。因为
List l=[]
确实有效……这看起来像是Groovy bug。
[]
有一个红色的类型
List
,这在我的PoV中很好。与
null
相同,在这种特殊情况下,具有类型为
List
的LHS,我不知怎的期望
[]
为相同类型。。。因为
List l=[]
确实有效……这看起来像Groovy bug。