digitalmars.D.learn - Top level associative arrays
- ANtlord (19/19) May 02 2017 Hello! Is it possible to define associative array on top level of
- ANtlord (3/22) May 02 2017 Sorry. There is should be `writeln(dict["s"]);` instead
- ANtlord (34/53) May 02 2017 By the way I notice some strange compile error when I try to
- evilrat (18/37) May 02 2017 Making enum means that value should be available at compile time
- ANtlord (5/22) May 02 2017 I know about D's enums and I know about module ctors but my
- evilrat (14/41) May 02 2017 Because it is perfectly fine. They are live in the module scope,
- Jacob Carlborg (5/22) May 02 2017 Note that when declared as "enum", all places it's referenced, a new
- ANtlord (4/6) May 02 2017 If it is allocated at all places I can move initialization to
- ANtlord (2/9) May 02 2017 I think it will be more suitable to create singleton of structure.
- H. S. Teoh via Digitalmars-d-learn (19/27) May 02 2017 Just declare it immutable. The module ctor can still initialize it,
- ANtlord (2/28) May 02 2017 Thanks a lot!
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.
May 02 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.Sorry. There is should be `writeln(dict["s"]);` instead `writeln(val);`
May 02 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`
May 02 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.Making enum means that value should be available at compile time and AA's are fully dynamic. But if my memory serves me well, you can declare empty AA and delay initialization. So the closest solution is to move initialization of AA to shared module ctor(note that there is difference between shared and non-shared, refer to documentation) such as in this example: -------------------------------- static shared this() // <-- module ctors run before main() { dict = [ "s": "q", "ss": "qq" ]; } string[string] dict; void main() { ... dict is already initialized ... }
May 02 2017
On Tuesday, 2 May 2017 at 08:24:09 UTC, evilrat wrote:Making enum means that value should be available at compile time and AA's are fully dynamic. But if my memory serves me well, you can declare empty AA and delay initialization. So the closest solution is to move initialization of AA to shared module ctor(note that there is difference between shared and non-shared, refer to documentation) such as in this example: -------------------------------- static shared this() // <-- module ctors run before main() { dict = [ "s": "q", "ss": "qq" ]; } string[string] dict; void main() { ... dict is already initialized ... }I know about D's enums and I know about module ctors but my question is about difference between array and associative array in case of definition in top level of module. Why DMD allows to define array and doesn't allow to define associative array.
May 02 2017
On Tuesday, 2 May 2017 at 09:50:50 UTC, ANtlord wrote:On Tuesday, 2 May 2017 at 08:24:09 UTC, evilrat wrote:Because it is perfectly fine. They are live in the module scope, which has its own life time, and from runtime or lifetime perspective there is no difference here. And since array can be fixed-sized it is valid to use as enum value. But there is one catch, in case of enum array it is best to avoid it in favor of immutable array* because every time you reference it it will allocate. But thats the difference between enum and not enum, not the array and map. This is what I remember from the past, and it is possibly that no longer relevant anymore. * not sure if it prevents allocation though, but in theory it should since it *should* go in to program data section when compilingMaking enum means that value should be available at compile time and AA's are fully dynamic. But if my memory serves me well, you can declare empty AA and delay initialization. So the closest solution is to move initialization of AA to shared module ctor(note that there is difference between shared and non-shared, refer to documentation) such as in this example: -------------------------------- static shared this() // <-- module ctors run before main() { dict = [ "s": "q", "ss": "qq" ]; } string[string] dict; void main() { ... dict is already initialized ... }I know about D's enums and I know about module ctors but my question is about difference between array and associative array in case of definition in top level of module. Why DMD allows to define array and doesn't allow to define associative array.
May 02 2017
On 2017-05-02 09:48, 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.Note that when declared as "enum", all places it's referenced, a new associative array will be allocated. -- /Jacob Carlborg
May 02 2017
On Tuesday, 2 May 2017 at 12:41:01 UTC, Jacob Carlborg wrote:Note that when declared as "enum", all places it's referenced, a new associative array will be allocated.If it is allocated at all places I can move initialization to module ctor as says evilrat but how can I make an immutable associative array?
May 02 2017
On Tuesday, 2 May 2017 at 14:37:20 UTC, ANtlord wrote:On Tuesday, 2 May 2017 at 12:41:01 UTC, Jacob Carlborg wrote:I think it will be more suitable to create singleton of structure.Note that when declared as "enum", all places it's referenced, a new associative array will be allocated.If it is allocated at all places I can move initialization to module ctor as says evilrat but how can I make an immutable associative array?
May 02 2017
On Tue, May 02, 2017 at 02:37:20PM +0000, ANtlord via Digitalmars-d-learn wrote:On Tuesday, 2 May 2017 at 12:41:01 UTC, Jacob Carlborg wrote:Just declare it immutable. The module ctor can still initialize it, because ctors are allowed to initialize immutables: ------ immutable string[string] dict; static this() { dict = [ "abc": "def", "ghi": "lmn" ]; } void main() { import std.stdio; writeln(dict["abc"]); } ------ T -- Just because you survived after you did it, doesn't mean it wasn't stupid!Note that when declared as "enum", all places it's referenced, a new associative array will be allocated.If it is allocated at all places I can move initialization to module ctor as says evilrat but how can I make an immutable associative array?
May 02 2017
On Tuesday, 2 May 2017 at 16:34:15 UTC, H. S. Teoh wrote:On Tue, May 02, 2017 at 02:37:20PM +0000, ANtlord via Digitalmars-d-learn wrote:Thanks a lot!On Tuesday, 2 May 2017 at 12:41:01 UTC, Jacob Carlborg wrote:Just declare it immutable. The module ctor can still initialize it, because ctors are allowed to initialize immutables: ------ immutable string[string] dict; static this() { dict = [ "abc": "def", "ghi": "lmn" ]; } void main() { import std.stdio; writeln(dict["abc"]); } ------ TNote that when declared as "enum", all places it's referenced, a new associative array will be allocated.If it is allocated at all places I can move initialization to module ctor as says evilrat but how can I make an immutable associative array?
May 02 2017