You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by MG <mg...@arscreat.com> on 2018/05/01 14:21:27 UTC

Re: [Poll] About supporting Java-like array

I have created the issue: 
https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-8566

Feel free to comment on the "less lenient for static compilation case ?" 
question:
"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 ?"

Cheers,
mg


On 30.04.2018 05:18, Paul King wrote:
> That sounds like a limitation we'd like to remove when using 
> @CompileStatic. Want to create a Jira?
>
> Cheers, Paul.
>
> On Mon, Apr 30, 2018 at 12:39 PM, MG <mgbiz@arscreat.com 
> <ma...@arscreat.com>> wrote:
>
>     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
>>     <http://groovy.329449.n5.nabble.com/Groovy-Users-f329450.html>
>>
>
>