Hi Daniel,
I did a quick check and it works with dynamic Groovy, but is rejected
under static compilation:
@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 a0instanceof int[]
assert aa0instanceof int[][]
assert aaa0instanceof int[][][]
assert aaa1instanceof int[][][]
assert aaa2instanceof int[][][]
assert aaa3instanceof int[][][]
}
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]]]
with @CompileStatic the compiler gives:
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[][][]
and one has to do
int[][] aa0 = [[1,2,3],[4,5,6]]as int[][]
etc
Cheers,
mg
On 30.04.2018 02:02, Daniel Sun wrote:
Hi mg,
As far as I remember, two dimensional array like`int[][] a = [[1, 2,
3], [4, 5, 6]]` will go wrong in the Groovy style.
Cheers,
Daniel.Sun
--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html