Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
None
-
None
Description
A compilation error occurs when using static compilation and attempting to call a method with a parameterized argument that contains an array in the type parameter. The equivalent Java code has no issues.
Here is a specific example:
@CompileStatic class ArrayGenericsIssue { static void main(String[] args) { Optional<Integer[]> o = Optional.of([1, 2, 3] as Integer[]) Integer firstFromArray = getFirstFromArray(o) //This fails to compile println "First (array): $firstFromArray" } static <E> E getFirstFromArray(Optional<E[]> optional) { return optional.get()[0] } }
The error returned is:
Error:(16, 30) Groovyc: [Static type checking] - Cannot call <E>ArrayGenericsIssue#getFirstFromArray(java.util.Optional <[Ljava.lang.Object;>) with arguments [java.util.Optional <[Ljava.lang.Integer;>
The expected behavior is that this code would compile and run successfully with @CompileStatic enabled.
Note that equivalent code with a non-array generic parameter works just fine:
static void main(String[] args) { Optional<List<Integer>> o = Optional.of([1, 2, 3]) Integer firstFromList = getFirstFromList(o) //This compiles just fine println "First (list): $firstFromList" } static <E> E getFirstFromList(Optional<List<E>> optional) { return optional.get()[0] }
Additionally, there is no compilation issue if the value is cast to a raw type:
Integer firstFromArray = getFirstFromArray((Optional) o2)
For some context, I'm running into this issue when working with jOOQ, as some of its API involves working with array type parameters.