Associative array literal. Why doesn't it compile?
BBasile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Aug 14 00:04:49 PDT 2015
On Friday, 14 August 2015 at 06:48:27 UTC, Adel Mamin wrote:
> Compiler dmd_2.068.0-0_amd64.deb on Ubuntu 12.04 Linux:
>
> auto lookup = [ "one":1, "two":2 ];
>
> The dmd error:
> Error: non-constant expression ["one":1, "two":2]
>
> Why doesn't it compile?
> As a workaround I could do the assignment one element at a time
> in a loop. It would be uglier though.
It's because of the key type (string is a library type). You have
to initialize it by hand. Explanation here:
http://stackoverflow.com/a/26862994/3661500
see the comment of Adam D. Ruppe. Although this case was 'a bit'
different since the AA had to be immutable (in this case it was
only possible to initialize it in a static module constructor or
in class/struct constructor). But it's almost the same problem.
More information about the Digitalmars-d-learn
mailing list