Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8342

Static compilation error with a method returning an array in a type parameter



      A compilation error occurs when using static compilation and attempting to assign the result of a method that returns a parameterized argument that contains an array in the type parameter. The equivalent Java code has no issues.

      Here is a specific example:

      class ArrayGenericsIssue {
        static void main(String[] args) {
          Optional<Integer[]> value = testArrayMethod(1) //This fails to compile
        static <E> Optional<E[]> testArrayMethod(E ignored) {
          return Optional.empty()

      The error returned is:

      Error:(11, 33) Groovyc: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Optional <E[]> to: java.util.Optional <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>> value = testListMethod(1)
        static <E> Optional<List<E>> testListMethod(E ignored) {
          return Optional.empty()

      Additionally, there is no compilation issue if the value is cast:

      Optional<Integer[]> value = (Optional<Integer[]>) testArrayMethod(1)

      For some context, I'm running into this issue when working with jOOQ, as some of its API involves working with array type parameters.




            • Assignee:
              paulk Paul King
              mjjustin M. Justin
            • Votes:
              0 Vote for this issue
              3 Start watching this issue


              • Created: