groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "M. Justin (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (GROOVY-8340) Static compilation with a method parameter containing an array in a type parameter
Date Wed, 04 Oct 2017 22:22:00 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-8340?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

M. Justin updated GROOVY-8340:
------------------------------
    Description: 
A compilation error occurs when 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:

{code}@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]
  }
}{code}

The error returned is:

{code}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;>{code}

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:
{code}
  static void main(String[] args) {
    Optional<List<Integer>> o2 = Optional.of([1, 2, 3])

    Integer firstFromList = getFirstFromList(o2) //This compiles just fine
    println "First (list): $firstFromList"
  }

  static <E> E getFirstFromList(Optional<List<E>> optional) {
    return optional.get()[0]
  }

{code}

Similarly, there is no compilation issue if the value is cast to a raw type:

{code}Integer firstFromArray = getFirstFromArray((Optional) o2){code}

  was:
A compilation error occurs when 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:

{code}@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]
  }
}{code}

The error returned is:

{code}Error:(16, 30) Groovyc: [Static type checking] - Cannot call <E> test.groovy.compilestatic.ArrayGenericsIssue#getFirstFromArray(java.util.Optional
<[Ljava.lang.Object;>) with arguments [java.util.Optional <[Ljava.lang.Integer;>{code}

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:
{code}
  static void main(String[] args) {
    Optional<List<Integer>> o2 = Optional.of([1, 2, 3])

    Integer firstFromList = getFirstFromList(o2) //This compiles just fine
    println "First (list): $firstFromList"
  }

  static <E> E getFirstFromList(Optional<List<E>> optional) {
    return optional.get()[0]
  }

{code}

Similarly, there is no compilation issue if the value is cast to a raw type:

{code}Integer firstFromArray = getFirstFromArray((Optional) o2){code}


> Static compilation with a method parameter containing an array in a type parameter
> ----------------------------------------------------------------------------------
>
>                 Key: GROOVY-8340
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8340
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>            Reporter: M. Justin
>              Labels: generics
>
> A compilation error occurs when 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:
> {code}@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]
>   }
> }{code}
> The error returned is:
> {code}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;>{code}
> 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:
> {code}
>   static void main(String[] args) {
>     Optional<List<Integer>> o2 = Optional.of([1, 2, 3])
>     Integer firstFromList = getFirstFromList(o2) //This compiles just fine
>     println "First (list): $firstFromList"
>   }
>   static <E> E getFirstFromList(Optional<List<E>> optional) {
>     return optional.get()[0]
>   }
> {code}
> Similarly, there is no compilation issue if the value is cast to a raw type:
> {code}Integer firstFromArray = getFirstFromArray((Optional) o2){code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message