June 23, 2010 Re: Cannot initialize associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wed, 23 Jun 2010 00:30:40 +0200, Ali Çehreli <acehreli@yahoo.com> wrote:
> dcoder wrote:
>
> > So, I moved the initialization to inside the main function, and now it works.
> > Great. I think we need to put this question in the FAQ.
>
> For future reference, if it really needs to be global:
>
> uint[string] mywords;
>
> static this()
> {
> mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
> }
>
> Ali
from what I have read in TDPL so far (about half way now), there is no mention of this limitation.
It just says that [key:value] is how you would statically initialize a AA.
Does this mean that it is just a D implementation issue. To me its seems like this should be a defined part
of the language. Also the compiler should really rewrite it to your above code anyway, surely?
-Rory
|
June 24, 2010 Re: Cannot initialize associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bernard Helyer | Bernard Helyer Wrote: > On Wed, 23 Jun 2010 00:41:45 -0700, Ali Çehreli wrote: > > > Ali Çehreli wrote: > >> dcoder wrote: > >> > >> > So, I moved the initialization to inside the main function, and now > >> it works. > >> > Great. I think we need to put this question in the FAQ. > >> > >> For future reference, if it really needs to be global: > >> > >> uint[string] mywords; > >> > >> static this() > >> { > >> mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ]; > >> } > > > > Could someone please verify whether the above is really necessary? Is it actually a dmd bug that we need to use 'static this()' to initialize an associative array? > > > > Ali > > I can't remember where exactly I read it, but there's a line in the docs specifically forbidding the use of AAs in constant expressions, so it's by design AFAIK. http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral "Associative array literals are a comma-separated list of key:value pairs... *An AssocArrayLiteral cannot be used to statically initialize anything. *" |
June 24, 2010 Re: Cannot initialize associative array. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic Wrote: > Bernard Helyer Wrote: > > > On Wed, 23 Jun 2010 00:41:45 -0700, Ali Çehreli wrote: > > > > > Ali Çehreli wrote: > > >> dcoder wrote: > > >> > > >> > So, I moved the initialization to inside the main function, and now > > >> it works. > > >> > Great. I think we need to put this question in the FAQ. > > >> > > >> For future reference, if it really needs to be global: > > >> > > >> uint[string] mywords; > > >> > > >> static this() > > >> { > > >> mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ]; > > >> } > > > > > > Could someone please verify whether the above is really necessary? Is it actually a dmd bug that we need to use 'static this()' to initialize an associative array? > > > > > > Ali > > > > I can't remember where exactly I read it, but there's a line in the docs specifically forbidding the use of AAs in constant expressions, so it's by design AFAIK. > > http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral > > "Associative array literals are a comma-separated list of key:value pairs... *An AssocArrayLiteral cannot be used to statically initialize anything. *" Woops, correct link: http://www.digitalmars.com/d/2.0/expression.html#AssocArrayLiteral |
Copyright © 1999-2021 by the D Language Foundation