September 19, 2006
Serg Kovrov wrote:
> I believe there are some issues with array literals right now, and I am sure they will be fixed.
> 
> Meantime, how this array literals should be used? there is not much example code in docs.
> 
> int[] i = [1, 2, 3];

This is the one, except the static initializor checks have not been culled yet, so it doesn't compile.  (This is a bug.)  For now, you would do something like this:

int[] i; i = [1, 2, 3];

> or
> int[] i = new [1, 2, 3];
> or
> int[] i = new int[1, 2, 3];
> Can it be used with auto i = [1, 2, 3] ?
> 
> How about array of stings?like:
> char[][] s = ["one", "two", "three"];

Just like this, once the static-init-chk is culled.

> The only syntax i managed to compile is
> int[] i = new int[1, 2, 3];
> but when i try to print it it produces "[0,0,0]"
> 
> Definitely a bug. But before report bugs I'd like to know how it actually should works.
> 

Try compiling/running the following:

# import std .stdio ;
#
# void main () {
#   writefln([1, 2, 3]);
#   writefln(["foo", "bar"]);
# }

-- Chris Nicholson-Sauls
September 19, 2006
Kyle Furlong wrote:
> Possibly Walter found it was easier to implement than he had expected?

Easier to implement than arguing about it <g>.
September 19, 2006
Walter Bright wrote:

> Kyle Furlong wrote:
> 
>> Possibly Walter found it was easier to implement than he had expected?
> 
> 
> Easier to implement than arguing about it <g>.

LOL guys, we are wearing him down!
September 20, 2006
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.
September 20, 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
September 20, 2006
Walter Bright wrote:
> Kyle Furlong wrote:
>> Possibly Walter found it was easier to implement than he had expected?
> 
> Easier to implement than arguing about it <g>.

Thats the spirit!

-- 
Kyle Furlong // Physics Undergrad, UCSB

"D is going wherever the D community wants it to go." - Walter Bright
September 20, 2006
David Medlock skrev:
> Walter Bright wrote:
> 
>> Kyle Furlong wrote:
>>
>>> Possibly Walter found it was easier to implement than he had expected?
>>
>>
>> Easier to implement than arguing about it <g>.
> 
> LOL guys, we are wearing him down!

So now is the time to be nagging about using properties as lvalues?

Great stuff, array literals have allowed me to cut many lines of codes and ugly hacks. Unfortunately I also have to wait for GDC :/. But some things are well worth waiting for.

// Fredrik Olsson
September 20, 2006
Walter Bright wrote:
> Array literals, by popular demand.
> 
> http://www.digitalmars.com/d/changelog.html

Walter, you freakin' rule!
1 2 3 4
Next ›   Last »