DMD 0.167 release
Reiner Pope
reiner.pope at REMOVE.THIS.gmail.com
Wed Sep 20 00:15:55 PDT 2006
Knud Sørensen wrote:
> On Mon, 18 Sep 2006 14:02:22 -0700, Walter Bright wrote:
>
>> clayasaurus wrote:
>>> Alright, is this feature supposed to work with multi-dimensional arrays
>>> as well? like...
>>>
>>> bob[][] = [[2,3], [4,2], [2,0]];
>>>
>>> I get 'invalid conversion from int[2][3] to int[][]'
>> Since T[n] and T[] are different types, one gets picked. But you can do:
>>
>> int[][] bob = [ cast(int[])[2,3], [4,2], [2,0] ];
>>
>> as the element type of the array is determined by the type of the first
>> element.
>
> Is it possible to let it initial number of ['s decide the dimension
>
> [1, -> int[]
> [[2, -> int[][]
> [[[3u, -> uint[][][]
> etc.
>
> So, that we don't the ugly cast.
Finding the dimension is not the problem. The point is really that,
although there is an implicit cast available from int[2] (a
statically-sized array) to int[] (a dynamic array), there is no implicit
cast available from int[2][3] to int[][] (multidimensional
statically-sized and dynamic arrays, respectively). This lack of
implicit cast is understandable, because casting in higher dimensions
would require creation of extra temporary pointers to the arrays
(although, on second thoughts, this conversion seems to cost no more
than the allocation of a dynamic array anyway...)
I think that, for cases like these where casts would require, we could
fall back on a slightly longer, yet unambiguous syntax: the syntax that
Chris Miller suggested a few months ago:
int[]![6,7,8,9]
This would be the most verbose form, but it would mean that arrays in
high dimensions could be declared like this:
int[][][]![ [ [1,2],[1,2] ],[ [1,2],[1,2] ] ]
instead of this:
[ cast(int[][])[ cast(int[])[1,2],[1,2] ],[ [1,2],[1,2] ] ]
which would require a cast at every dimension of the array.
Cheers,
Reiner
More information about the Digitalmars-d-announce
mailing list