Top level associative arrays

ANtlord via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 2 01:02:14 PDT 2017


On Tuesday, 2 May 2017 at 07:48:35 UTC, ANtlord wrote:
> Hello! Is it possible to define associative array on top level 
> of module?
> I try to compile this code and I get message `Error: 
> non-constant expression ["s":"q", "ss":"qq"]`
>
> import std.stdio;
>
> auto dict = [
> 	"s": "q",
> 	"ss": "qq"
> ];
> void main()
> {
> 	writeln(val);
> }
>
> I solved it by replacement of word `auto` by `enum`. It is 
> acceptable for me. But I notice some inconsistency of logic. 
> When I define simple array I don't get same compile error and 
> it doesn't lead to define this array using enum. What is key 
> difference between them in this case?
>
> Thanks. Sorry if my English is not clear.

By the way I notice some strange compile error when I try to 
change associatove array defined using enum.

import std.stdio;

enum dict = [
	"s": "q",
	"ss": "qq"
];

void main()
{
	dict["sss"] = "qqq";
}

Compilation of this code returns the error

& el:0x3237ab4 cnt=0 cs=0 &  TY* 0x3235794
  el:0x3235794 cnt=0 cs=0 call  TY* 0x3235744 0x32356f4
   el:0x3235744 cnt=0 cs=0 var  TYC func  _d_assocarrayliteralTX
   el:0x32356f4 cnt=0 cs=0 param  TYvoid 0x32356a4 0x3235654
    el:0x32356a4 cnt=0 cs=0 param  TYvoid 0x3234c44 0x3234d34
     el:0x3234c44 cnt=0 cs=0 rpair  TYucent 0x3234ba4 0x3234bf4
      el:0x3234ba4 cnt=0 cs=0 relconst  TY*  0+& _TMP8
      el:0x3234bf4 cnt=0 cs=0 const  TYuns long long 2LL
     el:0x3234d34 cnt=0 cs=0 rpair  TYucent 0x3234c94 0x3234ce4
      el:0x3234c94 cnt=0 cs=0 relconst  TY*  0+& _TMP5
      el:0x3234ce4 cnt=0 cs=0 const  TYuns long long 2LL
    el:0x3235654 cnt=0 cs=0 var  TY*  _D16TypeInfo_HAyaAya6__initZ
Internal error: backend/cgcs.c 352

But when I try to change simple array defined using enum

import std.stdio;

enum arr = [1, 2, 3];

void main()
{
	arr ~= 4;
}

I get the clear error `Error: [1, 2, 3] is not an lvalue`


More information about the Digitalmars-d-learn mailing list