Issue 17607 - not an associative array initializer
Summary: not an associative array initializer
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-05 19:28 UTC by Andre
Modified: 2022-12-17 10:37 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andre 2017-07-05 19:28:21 UTC
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 John Colvin 2017-10-30 16:12:15 UTC
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 Andre 2017-10-31 10:07:23 UTC
(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 John Colvin 2017-10-31 10:10:06 UTC
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 Seb 2018-04-02 04:14:21 UTC
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 Andre 2018-04-09 19:35:51 UTC
(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 Steven Schveighoffer 2022-12-02 15:51:40 UTC
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 Steven Schveighoffer 2022-12-03 03:36:40 UTC
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]
```