www.digitalmars.com         C & C++   DMDScript  

D - Initialising non-static and associative arrays

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
I've noticed a few oddities in what the compiler will accept in terms of 
array initialisation.

A declaration like

	int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];

is valid at the module level, or as a class member.  But within a 
function, the compiler doesn't like it:

	variable qwert is not a static and cannot have static initializer

This seems an arbitrary restriction.  Surely it should be allowed?  The 
same applies to struct initialisation, but I forget if that's quite the 
same.

And something that doesn't seem to be allowed anywhere:

	static int[int] yuiop = [ 4: 5, 10: 6, 69: 30 ];

	Error: cannot use array to initialize int[int]

What should I use to initialise it then? :-)

Seriously, I tried to use something of this form, but couldn't, and 
ended up using a switch.  Surely it should be possible to initialise 
associative arrays just like good old-fashioned linear arrays?

Stewart.

-- 
My e-mail is valid but not my primary mailbox, aside from its being the 
unfortunate victim of intensive mail-bombing at the moment.  Please keep 
replies on the 'group where everyone may benefit.
Mar 29 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Stewart Gordon wrote:

 I've noticed a few oddities in what the compiler will accept in terms 
 of array initialisation.

 A declaration like

     int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];

 is valid at the module level, or as a class member.  But within a 
 function, the compiler doesn't like it:

     variable qwert is not a static and cannot have static initializer

 This seems an arbitrary restriction.  Surely it should be allowed?  
 The same applies to struct initialisation, but I forget if that's 
 quite the same.

 And something that doesn't seem to be allowed anywhere:

     static int[int] yuiop = [ 4: 5, 10: 6, 69: 30 ];

     Error: cannot use array to initialize int[int]

 What should I use to initialise it then? :-)

 Seriously, I tried to use something of this form, but couldn't, and 
 ended up using a switch.  Surely it should be possible to initialise 
 associative arrays just like good old-fashioned linear arrays? .

You can use: const int[6] qwert = [ 9, 18, 28, 39, 51, 64 ]; But seriously this is another thing I'd like supported in D. -- -Anderson: http://badmama.com.au/~anderson/
Mar 29 2004
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
J Anderson wrote:

 You can use:
 const int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];

And thereby making it static. So long!
Mar 31 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Manfred Nowak wrote:

J Anderson wrote:

  

You can use:
const int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];
    

And thereby making it static. So long!

-- -Anderson: http://badmama.com.au/~anderson/
Mar 31 2004
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Stewart Gordon wrote:

[...]
 is valid at the module level, or as a class member.  But within a 
 function,

I see this simple reason: it is put onto the stack and therefore the initialization cannot prepared at compile time. Modules and classes do not recurse. So there is a difference. On the other hand I do not understand why it is allowed that the non static array can be initialized by a static array: static int[6] arr1=[1,2]; int[6] arr2= arr1; // no error here [...]
 Surely it should be allowed?

Maybe, but it would hide the runtime, that is involved. [associative array initialization]
 What should I use to initialise it then? :-)

D/14684 So long!
Mar 31 2004
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Manfred Nowak wrote:

 Stewart Gordon wrote:
 
 [...]
 
 is valid at the module level, or as a class member.  But within a 
 function,

I see this simple reason: it is put onto the stack and therefore the initialization cannot prepared at compile time. Modules and classes do not recurse. So there is a difference.

When instantiating a class, it clearly manages to create a copy of the initialisation data. So why can't it do the same when instantiating a stack frame for a function? Even C manages to do this!
 On the other hand I do not understand why it is allowed that the non 
 static array can be initialized by a static array:
 
    static int[6] arr1=[1,2];
    int[6] arr2= arr1;  // no error here 

Thanks for the workaround!
 [...]
 
 Surely it should be allowed?

Maybe, but it would hide the runtime, that is involved.

Pardon? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Apr 01 2004