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

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

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Static compilation

    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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            mjjustin M. Justin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: