www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - non-aa-initializer?

reply Steven Schveighoffer <schveiguy gmail.com> writes:
This is not an associative array initializer:

```d
['A' : ['B': 0]]
```

Yet, this is:

```d
(['A' : ['B': 0]])
```

I haven't figured out *why* this isn't a valid initializer, or why the 
parentheses are needed, but it has been this way for as far back as I 
can test on run.dlang.io.

A bug report exists on this. But any explanation of this behavior (bug 
or not) would be helpful: https://issues.dlang.org/show_bug.cgi?id=17607

-Steve
Dec 02 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 2 December 2022 at 18:26:36 UTC, Steven Schveighoffer 
wrote:
 This is not an associative array initializer:

 ```d
 ['A' : ['B': 0]]
 ```

 Yet, this is:

 ```d
 (['A' : ['B': 0]])
 ```
This looks like the nested objects I use in JSON and doesn't need parentheses because it's built with JSON. For example, without the parentheses, the first one will not compile, but will compile when the individual objects are created: ```d void main() { alias letter = int[char];/* letter[string] letters = [ "Upper" : ['A': 65], "Lower" : ['a': 97] ]; //* not compile */ letter[string] letters = ([ "Upper" : ['A': 65], "Lower" : ['a': 97] ]); // okay letter[] up = [['A': 65], ['B': 66], ['C': 67]]; letter[] low = [['a': 97], ['b': 98], ['c': 99]]; letter[string] lets = [ "Upper" : up[0], "Lower" : low[0] ]; // okay assert(lets == letters); } ``` SDB 79
Dec 02 2022
parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 2 December 2022 at 19:36:10 UTC, Salih Dincer wrote:
 This looks like the nested objects I use in JSON ...
Here's what I'm talking about: ```d import std.json; import std.stdio; void main() { alias nd = JSONValue; nd root; string[] A, I; A = ["a", "â", "A", "Â"]; I = ["ı", "î", "I", "Î"]; nd[string] let = [ "Lower" : nd([ "a letter" : nd( A[0..2] ), "ı letter" : nd( I[0..2] ) ]), "Upper" : nd([ "A letter" : nd( A[2..$] ), "I letter" : nd( I[2..$] ) ]) ]; root.object = ["Turkish" : nd( let )]; root.toJSON(true).writeln; } /* { "Turkish": { "Lower": { "a letter": [ "a", "â" ], "ı letter": [ "ı", "î" ] }, "Upper": { "A letter": [ "A", "Â" ], "I letter": [ "I", "Î" ] } } } */ ``` So you need to init nested AA at different times. If you want the compiler to do this in one line, we have to use parentheses. SDB 79
Dec 02 2022