Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 29, 2004 Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
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.
|
March 29, 2004 Re: Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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/ |
March 31, 2004 Re: Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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! |
March 31, 2004 Re: Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | J Anderson wrote:
> You can use:
> const int[6] qwert = [ 9, 18, 28, 39, 51, 64 ];
[...]
And thereby making it static.
So long!
|
March 31, 2004 Re: Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | 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! > > Whoops, missed that part. -- -Anderson: http://badmama.com.au/~anderson/ |
April 01, 2004 Re: Initialising non-static and associative arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | 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. <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. |
Copyright © 1999-2021 by the D Language Foundation