groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "mgroovy (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (GROOVY-8566) Array initialization from list literal without "as" should be supported under static compilation
Date Tue, 01 May 2018 14:17:00 GMT

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

mgroovy updated GROOVY-8566:
----------------------------
    Description: 
Dynamic Groovy supports initializing an array from a list literal:
{code:java}
@Test
@Ignore
void arrayFromListLiteral() {
  int[] a0 = [1,2,3]
  int[][] aa0 = [[1,2,3],[4,5,6]]
  int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]
  int[][][] aaa1 = [[1,2,3],[4,5,6]]
  int[][][] aaa2 = [1,2,3,4,5,6]
  int[][][] aaa3 = 1

  println "a0=$a0"
  println "aa0=$aa0"
  println "aaa0=$aaa0"
  println "aaa1=$aaa1"
  println "aaa2=$aaa2"
  println "aaa3=$aaa3"

  assert a0 instanceof int[]
  assert aa0 instanceof int[][]
  assert aaa0 instanceof int[][][]
  assert aaa1 instanceof int[][][]
  assert aaa2 instanceof int[][][]
  assert aaa3 instanceof int[][][]
}
{code}
gives:

a0=[1, 2, 3]
 aa0=[[1, 2, 3], [4, 5, 6]]
 aaa0=[[[1], [2], [3]], [[4], [5], [6]]]
 aaa1=[[[1], [2], [3]], [[4], [5], [6]]]
 aaa2=[[[1]], [[2]], [[3]], [[4]], [[5]], [[6]]]
 aaa3=[[[1]]]

Using @CompileStatic on the test the compiler gives:
{code:java}
Error:(37, 19) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][]
Error:(38, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.util.List> into array of type int[][][]
Error:(39, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][][]
Error:(40, 22) Groovyc: [Static type checking] - Cannot assign value of type int into array
of type int[][][]
Error:(41, 22) Groovyc: [Static type checking] - Cannot assign value of type int to variable
of type int[][][]
{code}
Adding the "as" operator:
{code:java}
@Test
@Ignore
void arrayFromListLiteral() {
  int[] a0 = [1,2,3]
  int[][] aa0 = [[1,2,3],[4,5,6]] as int[][]
  int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]  as int[][][]
  // etc
}
{code}
makes the code work the same as in the dynamic case.

In light of the upcoming Groovy 3.0 support for Java-style curly-braces literal array syntax, it
would be good to support the idiomatic Groovy array initialization syntax also for the static
compilation case.

An additonal question would be, whether in the static case the automatic conversion of the
RHS expression to fit the LHS array type should be less lenient, i.e. the list structure should
be required to conform to the array dimensions given on the left ?

  was:
Dynamic Groovy supports initializing an array from a list literal:

{code}
@Test
@Ignore
void arrayFromListLiteral() {
  int[] a0 = [1,2,3]
  int[][] aa0 = [[1,2,3],[4,5,6]]
  int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]
  int[][][] aaa1 = [[1,2,3],[4,5,6]]
  int[][][] aaa2 = [1,2,3,4,5,6]
  int[][][] aaa3 = 1

  println "a0=$a0"
  println "aa0=$aa0"
  println "aaa0=$aaa0"
  println "aaa1=$aaa1"
  println "aaa2=$aaa2"
  println "aaa3=$aaa3"

  assert a0 instanceof int[]
  assert aa0 instanceof int[][]
  assert aaa0 instanceof int[][][]
  assert aaa1 instanceof int[][][]
  assert aaa2 instanceof int[][][]
  assert aaa3 instanceof int[][][]
}
{code}

gives:

a0=[1, 2, 3]
aa0=[[1, 2, 3], [4, 5, 6]]
aaa0=[[[1], [2], [3]], [[4], [5], [6]]]
aaa1=[[[1], [2], [3]], [[4], [5], [6]]]
aaa2=[[[1]], [[2]], [[3]], [[4]], [[5]], [[6]]]
aaa3=[[[1]]]

Using @CompileStatic on the test the compiler gives:

{code}
Error:(37, 19) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][]
Error:(38, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.util.List> into array of type int[][][]
Error:(39, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][][]
Error:(40, 22) Groovyc: [Static type checking] - Cannot assign value of type int into array
of type int[][][]
Error:(41, 22) Groovyc: [Static type checking] - Cannot assign value of type int to variable
of type int[][][]
{code}

Adding the "as" operator:

{code}
@Test
@Ignore
void arrayFromListLiteral() {
  int[] a0 = [1,2,3]
  int[][] aa0 = [[1,2,3],[4,5,6]] as int[][]
  int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]  as int[][][]
  // etc
}
{code}  

makes the code work the same as in the dynamic case.

In light of the upcoming Groovy 3.0 support for Java-style literal array syntax ({...}), it
would be good to support the idiomatic Groovy  array initialization syntax also for the static
compilation case.

An additonal question would be, whether in the static case the automatic conversion of the
RHS expression to fit the LHS array type should be less lenient, i.e. the list structure should
be required to conform to the array dimensions given on the left ?


> Array initialization from list literal without "as" should be supported under static
compilation
> ------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8566
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8566
>             Project: Groovy
>          Issue Type: Planned Work
>          Components: Compiler
>    Affects Versions: 2.4.15
>            Reporter: mgroovy
>            Priority: Major
>              Labels: CompileStatic, array, initialization
>
> Dynamic Groovy supports initializing an array from a list literal:
> {code:java}
> @Test
> @Ignore
> void arrayFromListLiteral() {
>   int[] a0 = [1,2,3]
>   int[][] aa0 = [[1,2,3],[4,5,6]]
>   int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]
>   int[][][] aaa1 = [[1,2,3],[4,5,6]]
>   int[][][] aaa2 = [1,2,3,4,5,6]
>   int[][][] aaa3 = 1
>   println "a0=$a0"
>   println "aa0=$aa0"
>   println "aaa0=$aaa0"
>   println "aaa1=$aaa1"
>   println "aaa2=$aaa2"
>   println "aaa3=$aaa3"
>   assert a0 instanceof int[]
>   assert aa0 instanceof int[][]
>   assert aaa0 instanceof int[][][]
>   assert aaa1 instanceof int[][][]
>   assert aaa2 instanceof int[][][]
>   assert aaa3 instanceof int[][][]
> }
> {code}
> gives:
> a0=[1, 2, 3]
>  aa0=[[1, 2, 3], [4, 5, 6]]
>  aaa0=[[[1], [2], [3]], [[4], [5], [6]]]
>  aaa1=[[[1], [2], [3]], [[4], [5], [6]]]
>  aaa2=[[[1]], [[2]], [[3]], [[4]], [[5]], [[6]]]
>  aaa3=[[[1]]]
> Using @CompileStatic on the test the compiler gives:
> {code:java}
> Error:(37, 19) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][]
> Error:(38, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.util.List> into array of type int[][][]
> Error:(39, 22) Groovyc: [Static type checking] - Cannot assign value of type java.util.List
<java.lang.Integer> into array of type int[][][]
> Error:(40, 22) Groovyc: [Static type checking] - Cannot assign value of type int into
array of type int[][][]
> Error:(41, 22) Groovyc: [Static type checking] - Cannot assign value of type int to variable
of type int[][][]
> {code}
> Adding the "as" operator:
> {code:java}
> @Test
> @Ignore
> void arrayFromListLiteral() {
>   int[] a0 = [1,2,3]
>   int[][] aa0 = [[1,2,3],[4,5,6]] as int[][]
>   int[][][] aaa0 = [[[1],[2],[3]],[[4],[5],[6]]]  as int[][][]
>   // etc
> }
> {code}
> makes the code work the same as in the dynamic case.
> In light of the upcoming Groovy 3.0 support for Java-style curly-braces literal array
syntax, it would be good to support the idiomatic Groovy array initialization syntax also
for the static compilation case.
> An additonal question would be, whether in the static case the automatic conversion of
the RHS expression to fit the LHS array type should be less lenient, i.e. the list structure
should be required to conform to the array dimensions given on the left ?



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message