D - Initialising non-static and associative arrays
- Stewart Gordon (22/22) Mar 29 2004 I've noticed a few oddities in what the compiler will accept in terms of...
- J Anderson (6/23) Mar 29 2004 You can use:
- Manfred Nowak (4/6) Mar 31 2004 [...]
- J Anderson (4/14) Mar 31 2004 Whoops, missed that part.
- Manfred Nowak (14/18) Mar 31 2004 I see this simple reason: it is put onto the stack and therefore the
- Stewart Gordon (12/33) Apr 01 2004 When instantiating a class, it clearly manages to create a copy of the
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
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
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
Manfred Nowak wrote:J Anderson wrote:Whoops, missed that part. -- -Anderson: http://badmama.com.au/~anderson/You can use: const int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];[...] And thereby making it static. So long!
Mar 31 2004
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? :-)http://www.digitalmars.com/drn-bin/wwwnews?D/14684 So long!
Mar 31 2004
Manfred Nowak wrote:Stewart Gordon wrote: [...]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!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 hereThanks for the workaround![...]<snip> 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.Surely it should be allowed?Maybe, but it would hide the runtime, that is involved.
Apr 01 2004