www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Method for initialising an array of structs

reply Chris Williams <chris c2cube.com> writes:
D allows you to do something like:

uint[3] temp;
temp[] = 42;

Thus allowing you to set the initial state without having to write a loop.
Going from this, it seems like you should be able to do something like:

struct Test {
  uint a;
}
Test[3] temp;
temp[].a = 42;

Or:

temp[] = {
 a : 42
}

But neither of these seem to work. Is there a method for doing this?

If not then I guess I would like to offer this as an addition to the language
following from the existing syntax. It seems like you should be able to do it,
given the way an array of a basic type can be handled.

Thank you,
Chris
Jan 15 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Chris Williams wrote:
 D allows you to do something like:
 
 uint[3] temp;
 temp[] = 42;
 
 Thus allowing you to set the initial state without having to write a loop.
 Going from this, it seems like you should be able to do something like:
 
 struct Test {
   uint a;
 }
 Test[3] temp;
 temp[].a = 42;
 
 Or:
 
 temp[] = {
  a : 42
 }
 
 But neither of these seem to work. Is there a method for doing this?
 
 If not then I guess I would like to offer this as an addition to the language
 following from the existing syntax. It seems like you should be able to do it,
 given the way an array of a basic type can be handled.
 
 Thank you,
 Chris
This might make potential sense as a new addition to ArrayOps, one of the features deferred to "2.0". It means being able to do things like (array1[] += array2[]) so its in roughly the same ballpark. I know I wouldn't mind having it. (It'd make things like delegate arrays rather nifty as well... (array[](foo, bar);).) -- Chris Nicholson-Sauls
Jan 15 2007