Cannot initialize associative array.
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Jun 24 07:05:52 PDT 2010
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. *"
More information about the Digitalmars-d-learn
mailing list