I build an AWS SDK for D based on the AWS definitions. I generate structures for the input/output data. These structures I fill with the structure initializer which works quite fine for anything except associate arrays. This is the minimized structure to create an record in an AWS Dynamo DB table. For the record you specify the attribute values via an associative array. (I generate an associative array as in the AWS API definition an JSON Object is used). Although structure initializer works quite well for array, it does not work for associative arrays. test.d(19): Error: not an associative array initializer The coding below does not compile. Instead I have to write much more verbose coding to fill the item. Indepent of the current ongoing DIP, is there any chance to enable structure initializer for associative arrrays? struct PutItemRequest { string tableName; AttributeValue[string] item; } struct AttributeValue { bool BOOL; string S; AttributeValue[string] M; string[] SS; } void main() { PutItemRequest request = { tableName: "table1", item: [ "field1": {S: "LALA"}, "field2": {SS: ["A", "B", "C"]}, "field3": { M: ["fieldA": {S: "234"}] } ] }; }
Another simpler case of this: struct S { //Error: not an associative array initializer D a = ["fdsa": ["fdsafd": "fdsfa"]]; // OK D b = (["fdsa": ["fdsafd": "fdsfa"]]); } struct D { this(string[string][string]) {} } Andre, perhaps putting brackets around it is a workaround for you as well?
(In reply to John Colvin from comment #1) > Another simpler case of this: > > struct S > { > //Error: not an associative array initializer > D a = ["fdsa": ["fdsafd": "fdsfa"]]; > > // OK > D b = (["fdsa": ["fdsafd": "fdsfa"]]); > } > > struct D > { > this(string[string][string]) {} > } > > > Andre, perhaps putting brackets around it is a workaround for you as well? thanks for the workaround. The Amazon Web Service request structures are quite complex. They have multiple fields with multiple hierarchies. Every additional characters makes it more difficult to read for other developers using the AWS library. If the syntax could be enhanced, this would really great.
My use case involves structures with these initialisers being used by people who aren't really D programmers, so it looks bad and is confusing to have the extra `( ... )` for me too. Hopefully there is a solution. I don't think it's a case of needing improved syntax in the language, it's just a compiler bug.
FYI: while it doesn't fix this bug, this PR is still very related this this enhancement request: https://github.com/dlang/dmd/pull/8051
(In reply to John Colvin from comment #3) > My use case involves structures with these initialisers being used by people > who aren't really D programmers, so it looks bad and is confusing to have > the extra `( ... )` for me too. > > Hopefully there is a solution. I don't think it's a case of needing improved > syntax in the language, it's just a compiler bug. I just have another look. The workaround is unfortunately not working but causing a lot of errors: struct PutItemRequest { AttributeValue[string] item; } struct AttributeValue { string S; } void main() { PutItemRequest request = { item: ([ "field1": {S: "LALA"} ]) }; } test2.d(15): Error: found } when expecting ; following statement test2.d(16): Error: found ] instead of statement test2.d(17): Error: found ; when expecting , test2.d(18): Error: expression expected, not } test2.d(18): Error: key:value expected for associative array literal test2.d(18): Error: found EOF when expecting , test2.d(14): Error: found EOF when expecting ] test2.d(14): Error: found EOF when expecting ) test2.d(18): Error: found end of file instead of initializer test2.d(18): Error: semicolon expected, not EOF test2.d(18): Error: found EOF when expecting } following compound statement
Just ran into this. Note the problem has nothing to do with structs: ```d int[char][char] arr = ['A' : ['B': 0]] ; // error int[char][char] arr = (['A' : ['B': 0]]); // ok ``` Does anyone have an actual explanation of why this is happening? It looks like an AA initializer to me.
This is most definitely a bug: ```d // int[char][char] arr = ['A' : ['B': 0]]; // error auto arr = ['A' : ['B': 0]]; // ok pragma(msg, typeof(arr)); // int[char][char] ```