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.
(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