Bug 17607 – not an associative array initializer

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-05T19:28:21Z
Last change time
2024-12-13T18:53:11Z
Assigned to
No Owner
Creator
Andre
Moved to GitHub: dmd#17804 →

Comments

Comment #0 by andre — 2017-07-05T19:28:21Z
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"}] } ] }; }
Comment #1 by john.loughran.colvin — 2017-10-30T16:12:15Z
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?
Comment #2 by andre — 2017-10-31T10:07:23Z
(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.
Comment #3 by john.loughran.colvin — 2017-10-31T10:10:06Z
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.
Comment #4 by greensunny12 — 2018-04-02T04:14:21Z
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
Comment #5 by andre — 2018-04-09T19:35:51Z
(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
Comment #6 by schveiguy — 2022-12-02T15:51:40Z
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.
Comment #7 by schveiguy — 2022-12-03T03:36:40Z
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] ```
Comment #8 by robert.schadek — 2024-12-13T18:53:11Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17804 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB